Java EnumSet Example

Java EnumSet class implements Set and uses it with enum types. EnumSet (as the name suggests) can contain only enum values and all the values belong to the same enum. In addition, EnumSet does not permit null values which means it throws a NullPointerException in attempt to add null values. It is not thread-safe which means if required, we need to synchronize it externally.

java-featured-image

Inheritance Diagram

Enum Set inheritance Diagram

EnumSet

Why EnumSet

Whenever we have to store enum values, EnumSet should always be the best Set Implementation. All basic operations are executed in a constant time(1) complexity, which is very fast. This is due to the fact that all methods in the EnumSet class are implement using arithmetic bitwise operations (<<, >>, &, |, etc.). EnumSet all in all, is very efficient because it uses less memory and also is fast.




Methods in EnumSet

  1. EnumSet<E> close(): returns a copy of the current set.
  2. static <E extends Enum<E>> EnumSet<E> allOf(Class<E> elementType): creates an enum set that contains all of the elements in the specified element type.
  3. static <E extends Enum<E>> EnumSet<E> complementOf(EnumSet<E> s): creates an enum set with the same element type as the specified enum set, initially containing all the elements of this type that are notcontained in the specified set.
  4. static <E extends Enum<E>> EnumSet<E> copyOf(Collection<E> c): creates a set initialized from the specified collection.
  5. static <E extends Enum<E>> EnumSet<E> copyOf(EnumSet<E> s): creates an enum set with the same element type as the specified set, containing the same elements, if there are any.
  6. static <E extends Enum<E>> EnumSet<E> of(E e): creates an enum set that contains the specified element.
  7. static <E extends Enum<E>> EnumSet<E> range(E from, E to): creates an enum set that contains all of the elements in the ranged specified by the two arguments.

Methods inherited from class java.util.AbstractSet:

equalshashCoderemoveAll

Methods inherited from class java.util.AbstractCollection:

addaddAllclearcontainscontainsAllisEmptyiteratorremoveretainAllsizetoArraytoArraytoString

Methods inherited from class java.lang.Object:

finalizegetClassnotifynotifyAllwaitwaitwait

Methods inherited from interface java.util.Set:

addaddAllclearcontainscontainsAllisEmptyiteratorremoveretainAllsizetoArraytoArray

For more information on the main methods of EnumSet, feel free to visit the original Oracle documentation.

import java.util.EnumSet; 
     
enum Student  
{ 
    NAME, AGE, MAJOR, YEAR 
}; 
public class EnumSetExample
{ 
    public static void main(String[] args)  
    { 
        // initializing set
        EnumSet<Student> set1, set2, set3;
  
        // populating the sets using of(E e)
        enumSet1 = EnumSet.of(Student.NAME, Student.MAJOR, Student.YEAR);
        // will get all of the properties that have not been initialized to the 
        // specified set (if any) 
        enumSet2 = EnumSet.complementOf(enumSet1); 
        // will get all of the properties that are present in the enumset
        enumSet3 = EnumSet.allOf(Student.class); 
        // will get from age to year and everything in between the properties of the 
        // enumset class
        enumSet4 = EnumSet.range(Student.AGE, Student.YEAR); 
        System.out.println("Set 1: " + enumSet1); 
        System.out.println("Set 2: " + enumSet2); 
        System.out.println("Set 3: " + enumSet3); 
        System.out.println("Set 4: " + enumSet4); 
    } 
}

Output:

Set 1: {NAME, MAJOR, YEAR}
Set 2: {AGE}
Set 3: {NAME, AGE, MAJOR, YEAR}
Set 4: {AGE, MAJOR, YEAR}

 

 

0 0 votes
Article Rating
guest
0 Comments
Inline Feedbacks
View all comments