Java EnumSet Class

Introduction

The EnumSet class in Java is a part of the java.util package.

Is is a specialized Set implementation designed for use with enum types.

Table of Contents

  1. What is the EnumSet Class?
  2. Common Methods
  3. Examples of Using the EnumSet Class
  4. Conclusion

1. What is the EnumSet Class?

EnumSet is a high-performance set implementation for enum types. It is more efficient than other set implementations for enums, offering fast access and minimal memory usage.

2. Common Methods

  • of(E first, E... rest): Creates an EnumSet with the specified elements.
  • add(E e): Adds the specified element to the set.
  • remove(Object o): Removes the specified element from the set.
  • contains(Object o): Returns true if the set contains the specified element.
  • allOf(Class<E> elementType): Creates an EnumSet containing all elements of the specified enum type.
  • noneOf(Class<E> elementType): Creates an empty EnumSet for the specified enum type.
  • range(E from, E to): Creates an EnumSet containing all elements in the specified range.

3. Examples of Using the EnumSet Class

Example 1: Creating and Using an EnumSet

This example demonstrates how to create and use an EnumSet with enum elements.

import java.util.EnumSet; enum Day { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY } public class EnumSetExample { public static void main(String[] args) { EnumSet<Day> workdays = EnumSet.of(Day.MONDAY, Day.TUESDAY, Day.WEDNESDAY); System.out.println("Workdays: " + workdays); } } 

Output:

Workdays: [MONDAY, TUESDAY, WEDNESDAY] 

Example 2: Adding and Removing Elements

This example shows how to add and remove elements from an EnumSet.

import java.util.EnumSet; enum Day { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY } public class ModifyEnumSetExample { public static void main(String[] args) { EnumSet<Day> days = EnumSet.noneOf(Day.class); days.add(Day.FRIDAY); days.add(Day.THURSDAY); System.out.println("Days after adding: " + days); days.remove(Day.THURSDAY); System.out.println("Days after removing Thursday: " + days); } } 

Output:

Days after adding: [THURSDAY, FRIDAY] Days after removing Thursday: [FRIDAY] 

Example 3: Using allOf and range

This example demonstrates how to create an EnumSet with all elements and a range of elements.

import java.util.EnumSet; enum Day { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY } public class AllOfRangeExample { public static void main(String[] args) { EnumSet<Day> allDays = EnumSet.allOf(Day.class); System.out.println("All days: " + allDays); EnumSet<Day> midWeek = EnumSet.range(Day.TUESDAY, Day.THURSDAY); System.out.println("Mid-week days: " + midWeek); } } 

Output:

All days: [MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY] Mid-week days: [TUESDAY, WEDNESDAY, THURSDAY] 

4. Conclusion

EnumSet is an efficient and type-safe set implementation for enum elements, providing fast access and minimal memory overhead. It is ideal for scenarios where the elements are known at compile time and are limited to enum types.

Leave a Comment

Scroll to Top