Signup/Sign In

Enum Set


In Java, EnumSet extends the AbstractSet and is implemented in the Set interface. This class is the member of the Java Collection Framework and also it can not be synchronized. EnumDet is faster than HashSet.

Syntax:

	
public abstract class EnumSet<E extends Enum<E>>
	

Below are the methods of EnumSet Class


S.no. Method Description
1 EnumSet of(E e1) It is used to create an Enum set using specified elements.
2 EnumSetcomplementOf(EnumSet s) It is used to create an Enum set using specified elements which are unique.
3 EnumSetallOf(Class elementType) It is used to create an Enum set using all the elements of the class.
4 EnumSet range(E from, E to) It is used to get the Enum elements of the given range.
5 EnumSetcopyof() It is used to copy the elements from the collection in a new enum set.

Example:

	
import java.util.EnumSet; 

enum Demo1
{ 
    RED, BLACK, BLUE, PINK, WHITE
}; 
public class EnumDemo1
{ 
    public static void main(String[] args)  
    { 
        EnumSet1<Demo1> a1, a2, a3, a4; 
        a1 = EnumSet1.of(Demo1.RED, Demo1.BLACK, Demo1.BLUE, Demo1.PINK, Demo1.WHITE); 
        a2 = EnumSet1.complementOf(a1); 
        a3 = EnumSet1.allOf(Demo1.class); 
        a4 = EnumSet1.range(Demo1.RED, Demo1.PINK); 
		System.out.println("a 1: " + a1); 
		System.out.println("a 2: " + a2); 
		System.out.println("a 3: " + a3); 
		System.out.println("a 4: " + a4); 
    } 
}
	
enum-set