public abstract class EnumSet<E extends Enum<E>>
extends AbstractSet<E>
implements Cloneable, Serializable
EnumSet can be explained with following points:
- EnumSet is a specialized Set implementation to be used with enum types.
- All of the elements in an enum set must come from a single enum type that is specified, explicitly or implicitly, when the set is created. Adding an instance of different enum will result in compile time error, as EnumSet provide type-safety.
- Enum sets are represented internally as bit vectors.
- The iterator returned by the iterator method traverses the elements in their natural order (the order in which the enum constants are declared).
- The returned iterator is weakly consistent: it will never throw ConcurrentModificationException and it may or may not show the effects of any modifications to the set that occur while the iteration is in progress.
- EnumSet does not permit Null elements. Attempts to insert a null element will throw NullPointerException. Attempts to test for the presence of a null element or to remove one will, however, function properly.
- EnumSet is not synchronized. If multiple threads access an enum set concurrently, and at least one of the threads modifies the set, it should be synchronized externally. This is typically accomplished by synchronizing on some object that naturally encapsulates the enum set. If no such object exists, the set should be “wrapped” using the Collections.synchronizedSet(java.util.Set) method. This is best done at creation time, to prevent accidental unsynchronized access:
Set<MyEnum> s = Collections.synchronizedSet(EnumSet.noneOf(MyEnum.class));
- All basic operations execute in constant time. They are likely (though not guaranteed) to be much faster than their HashSet counterparts. Even bulk operations execute in constant time if their argument is also an EnumSet.
- EnumSet should always be preferred over any other Set implementation when we are storing enum values.
- EnumSet is an abstract class, and it provides static factory method to create an instance of EnumSet e.g. EnumSet.of(…).
- EnumSet is an abstract class and it provides two concrete implementations, java.util.RegularEnumSet and java.util.JumboEnumSet. The main difference between the two is that the former uses a long variable to store elements while later uses a long[] to store its element. Since RegularEnumSet uses long variable, which is a 64 bit data type, it can only hold that much of an element. It chooses RegularEnumSet if the number of enum instances in Key Enum is less than or equal to 64, else it chooses JumboEnumSet.
EnumSet example
import java.util.EnumSet; import java.util.Iterator; import java.util.Set; enum days { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY } public class EnumSetExample { public static void main(String[] args) { Set<days> set = EnumSet.of(days.FRIDAY, days.SATURDAY); Iterator<days> iter = set.iterator(); while (iter.hasNext()) System.out.println(iter.next()); } }
Output
FRIDAY
SATURDAY
Set with all week Days:[SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY]
Set with none week Days:[]
EnumSet allOf() and noneOf() example
import java.util.*; enum days { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY } public class EnumSetExample { public static void main(String[] args) { Set<days> setAllDays = EnumSet.allOf(days.class); System.out.println("Week Days:"+set1); Set<days> setNoneDays = EnumSet.noneOf(days.class); System.out.println("Week Days:"+set2); } }
Output
FRIDAY
SATURDAY
Set with all week Days:[SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY]
Set with none week Days:[]
EnumSet add() example
import java.io.FileNotFoundException; import java.util.EnumSet; enum KeysEnum { KEY1, KEY2, KEY3}; public class EnumSetAddExample { public static void main(String[] args) throws FileNotFoundException { //EnumSet with two keys EnumSet<KeysEnum> enumSet = EnumSet.of(KeysEnum.KEY1, KeysEnum.KEY2); System.out.println(enumSet); //add another key enumSet.add(KeysEnum.KEY3); System.out.println(enumSet); } }
Output
[KEY1, KEY2]
[KEY1, KEY2, KEY3]