In Java, the EnumSet is a specialized set implementation for use with enum types. It is a part of java.util package and provides a highly optimized set for storing enum constants. The EnumSet is one of the specialized implementations of the Set interface for use with the enumeration type.
EnumSet is highly efficient and faster for enum types
All of the elements in an EnumSet must come from a single enumeration type that is specified when the set is created either explicitly or implicitly.
EnumSet uses a weakly consistent iterator, not a fail-fast one—so it doesn’t throw ConcurrentModificationException and may reflect some modifications during iteration.
Java
importjava.util.EnumSet;enumStudent{Geek1,Geek2,Geek3,Geek4,Geek5}publicclassGeeks{publicstaticvoidmain(String[]args){// Create an EnumSet containing specific elementsEnumSet<Student>e=EnumSet.of(Student.Geek1,Student.Geek2,Student.Geek3);System.out.println("EnumSet: "+e);}}
Output
EnumSet: [Geek1, Geek2, Geek3]
Hierarchy of EnumSet
The EnumSet class extends AbstractSet and implements the Set interface, forming part of the Java Collections Framework hierarchy designed specifically for enum types.
EnumSet
Declaration of EnumSet
In Java, the declaration of EnumSet can be done as:
EnumSet<Type> name = EnumSet.of(Element1, Element2, Element3);
Parameter: EnumSet.of() take one or more enum constants as arguments and specifying which enum values to include in the set.
Return Type: It returns a set containing the specified enum constants
Creates an enum set with the same element type as the specified enum set, initially containing all the elements of this type that are not contained in the specified set.
Creates an enum set initially containing all of the elements in the range defined by the two specified endpoints.
Example: This example demonstrates how to create and manipulate EnumSet using methods like of(), complementOf(), allOf() and range() with enum constants.
Java
importjava.util.EnumSet;// Enumenume{CODE,LEARN,CONTRIBUTE,QUIZ,MCQ};publicclassGeeks{publicstaticvoidmain(String[]args){// Creating a setEnumSet<e>s1,s2,s3,s4;// Adding elementss1=EnumSet.of(e.QUIZ,e.CONTRIBUTE,e.LEARN,e.CODE);s2=EnumSet.complementOf(s1);s3=EnumSet.allOf(e.class);s4=EnumSet.range(e.CODE,e.CONTRIBUTE);// Printing corresponding elements in SetsSystem.out.println("Set 1: "+s1);System.out.println("Set 2: "+s2);System.out.println("Set 3: "+s3);System.out.println("Set 4: "+s4);}}
Output
Set 1: [CODE, LEARN, CONTRIBUTE, QUIZ]
Set 2: [MCQ]
Set 3: [CODE, LEARN, CONTRIBUTE, QUIZ, MCQ]
Set 4: [CODE, LEARN, CONTRIBUTE]
Creating a EnumSet
In Java, EnumSet is an abstract class, we can not directly create an instance of it. Instead it provides static factory methods to create instances. There are two different internal implementations of EnumSet which are listed below:
RegularEnumSet: It uses a single long object to store the elements of the EnumSet. Each bit in the long value represents an enum constant. The size of a long is 64 bits, RegularEnumSet can store up to 64 enum values.
JumboEnumSet: It uses an array of long values to store the elements of the EnumSet. The key difference is that JumboEnumSet allows storing more than 64 values by using multiple long elements to represent a larger set of enum constants.
Note: These are package-private and backed by a bit vector.
Creates an EnumSet containing the specified enum constants.
Implementation of Internal Logic
If the number of elements (universe.length) is less than or equal to 64, RegularEnumSet is used otherwise JumboEnumSet is used for larger enum types.
if (universe.length <= 64) return new RegularEnumSet<>(elementType, universe); else return new JumboEnumSet<>(elementType, universe);
Example: This example demonstrates how to create an EnumSet using the EnumSet.allOf() method and print sites contents.
Java
importjava.util.*;classGeeks{// EnumenumGame{CRICKET,HOCKEY,TENNIS}publicstaticvoidmain(String[]args){// Creating an EnumSet using allOf()EnumSet<Game>g=EnumSet.allOf(Game.class);// Printing EnumSet elements to the consoleSystem.out.println("EnumSet: "+g);}}
Output
EnumSet: [CRICKET, HOCKEY, TENNIS]
Operations on EnumSet
1. Adding Elements
We can use add() and addAll() to insert elements to an EnumSet.
Java
importjava.util.EnumSet;classGeeks{// EnumenumGame{CRICKET,HOCKEY,TENNIS}publicstaticvoidmain(String[]args){// Creating an EnumSet// using allOf()EnumSet<Game>g1=EnumSet.allOf(Game.class);// Creating an EnumSet// using noneOf()EnumSet<Game>g2=EnumSet.noneOf(Game.class);// Using add() methodg2.add(Game.HOCKEY);// Printing the elements to the consoleSystem.out.println("EnumSet Using add(): "+g2);// Using addAll() methodg2.addAll(g1);// Printing the elements to the consoleSystem.out.println("EnumSet Using addAll(): "+g2);}}
Output
EnumSet Using add(): [HOCKEY]
EnumSet Using addAll(): [CRICKET, HOCKEY, TENNIS]
importjava.util.EnumSet;importjava.util.Iterator;classGeeks{// EnumenumGame{CRICKET,HOCKEY,TENNIS}publicstaticvoidmain(String[]args){// Creating an EnumSet using allOf()EnumSet<Game>g=EnumSet.allOf(Game.class);// Creating an iterator on gamesIterator<Game>i=g.iterator();System.out.print("EnumSet: ");while(i.hasNext()){// Iterating and printing elements to// the console using next() methodSystem.out.print(i.next());System.out.print(", ");}}}
Output
EnumSet: CRICKET, HOCKEY, TENNIS,
3. Removing Elements
We can use the remove() and removeAll() to remove elements from an EnumSet.
Java
importjava.util.EnumSet;classGeeks{// EnumenumGame{CRICKET,HOCKEY,TENNIS}publicstaticvoidmain(String[]args){// Creating EnumSet using allOf()EnumSet<Game>g=EnumSet.allOf(Game.class);// Printing the EnumSetSystem.out.println("EnumSet: "+g);// Using remove()booleanb=g.remove(Game.CRICKET);System.out.println("Is CRICKET removed? "+b);// Using removeAll() and storing the boolean resultbooleanb2=g.removeAll(g);System.out.println("Are all elements removed? "+b2);}}
Output
EnumSet: [CRICKET, HOCKEY, TENNIS]
Is CRICKET removed? true
Are all elements removed? true
Some other methods of classes or interfaces are defined below that somehow helps us to get a better understanding of AbstractSet Class as follows:
You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.