Collections

EnumSet in Java Code With Examples

Table of Contents

Program

import java.util.EnumSet;
import java.util.Set;
enum days {SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY}
public class EnumSetExamples 
{
	public static void main(String[] args)
	 {      
                // allof() 
		Set<days> set1 = EnumSet.allOf(days.class); 	                    	
		System.out.println("Week Days:" + set1);
		
                // noneof()
                Set<days> set2 = EnumSet.noneOf(days.class); 	   	            
		System.out.println("\nWeek Days:" + set2);
		EnumSet<days> set;
		
                // range()
                set = EnumSet.range(days.WEDNESDAY, days.SATURDAY);	            
		System.out.println("\nRange :" + set);
		EnumSet<days> set3 = EnumSet.allOf(days.class);
		System.out.println("\nBefore Cloning:" + set3);
		
                // clone()
                EnumSet<days> set4 = set3.clone();			
		System.out.println("\nAfter Cloning :" + set4);
		
                // remove()
                set1.remove(days.TUESDAY);
		System.out.println("\nAfter Removal:" + set1);		
	}
}

Output

Week Days:[SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY]

Week Days:[]

Range :[WEDNESDAY, THURSDAY, FRIDAY, SATURDAY]

Before Cloning:[SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY]

After Cloning :[SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY]

After Removal:[SUNDAY, MONDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY]

Description

public static > EnumSet allOf​(Class elementType)

public static > EnumSet allOf​(Class elementType)

Type Parameters:

E – The class of the elements in the set

Parameters:

elementType – the class object of the element type for this enum set

Returns:

An enum set containing all the elements in the specified type.

Throws:

NullPointerException – if elementType is null

public static > EnumSet noneOf​(Class elementType)

Creates an empty enum set with the specified element type.

Type Parameters:

E – The class of the elements in the set

Parameters:

elementType – the class object of the element type for this enum set

Returns:

An empty enum set of the specified type.

Throws:

NullPointerException – if elementType is null

public static > EnumSet range​(E from, E to)

Creates an enum set initially containing all of the elements in the range defined by the two specified endpoints. The returned set will contain the endpoints themselves, which may be identical but must not be out of order.

Type Parameters:

E – The class of the parameter elements and of the set

Parameters:

from – the first element in the range
to – the last element in the range

Returns:

an enum set initially containing all of the elements in the range defined by the two specified endpoints

Throws:

NullPointerException – if from or to are null
IllegalArgumentException – if from.compareTo(to) > 0

public EnumSet clone()

Returns a copy of this set.

Overrides:

clone in class Object

Returns:

a copy of this set

boolean remove​(Object o)

Removes the specified element from this set if it is present (optional operation). More formally, removes an element e such that Objects.equals(o, e), if this set contains such an element. Returns true if this set contained the element (or equivalently, if this set changed as a result of the call). (This set will not contain the element once the call returns.)

Specified by:

remove in interface Collection<E>

Parameters:

o – object to be removed from this set, if present

Returns:

true if this set contained the specified element

Throws:

ClassCastException – if the type of the specified element is incompatible with this set (optional)
NullPointerException – if the specified element is null and this set does not permit null elements (optional)
UnsupportedOperationException – if the remove operation is not supported by this set