Menu
Topics Index
...
`


Collections Framework > Collection Classes >
Siva Nookala - 03 Mar 2017
EnumSet extends AbstractSet and implements Set Interface In Java. EnumSet quickly turns any Enum elements into a Set. 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. Enum sets are represented internally as bit vectors. Null elements are not permitted. 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.

Declartion of EnumSet :
public abstract class EnumSet<E extends Enum<E>>

extends AbstractSet<E>

implements Cloneable, Serializable


Note : EnumSet defines no constructors.
Methods of EnumSet :
Method Description
static <E extends Enum<E>> EnumSet<E> allOf(Class<E> elementType) This method creates an enum set containing all of the elements in the specified element type.
EnumSet<E> clone() This method returns a copy of this set.
static <E extends Enum<E>> EnumSet<E> complementOf(EnumSet<E> s) This method 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.
static <E extends Enum<E>> EnumSet<E> copyOf(Collection<E> c) This method creates an enum set initialized from the specified collection.
static <E extends Enum<E>> EnumSet<E> copyOf(EnumSet<E> s) This method creates an enum set with the same element type as the specified enum set, initially containing the same elements (if any).
static <E extends Enum<E>> EnumSet<E> noneOf(Class<E> elementType) This method creates an empty enum set with the specified element type.
static <E extends Enum<E>> EnumSet<E> of(E e) This method creates an enum set initially containing the specified element.
static <E extends Enum<E>> EnumSet<E> of(E first, E... rest) This method creates an enum set initially containing the specified elements.
static <E extends Enum<E>> EnumSet<E> of(E e1, E e2) This method creates an enum set initially containing the specified elements.
static <E extends Enum<E>> EnumSet<E> of(E e1, E e2, E e3) This method creates an enum set initially containing the specified elements.
static <E extends Enum<E>> EnumSet<E> of(E e1, E e2, E e3, E e4) This method creates an enum set initially containing the specified elements.
static <E extends Enum<E>> EnumSet<E> of(E e1, E e2, E e3, E e4, E e5) This method creates an enum set initially containing the specified elements.
static <E extends Enum<E>> EnumSet<E> range(E from, E to) This method creates an enum set initially containing all of the elements in the range defined by the two specified endpoints.
EnumSetDemo
import java.util.*;

class EnumSetDemo
{
    enum Weekdays
    {
        Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday;
    }
    
    public static void main(String[] args)
    {
        EnumSet set = null; // LINE A
        System.out.println(set);
    
        set = EnumSet.of(Weekdays.Monday); // LINE B
        System.out.println("set when added monday : " + set);
    
        set = EnumSet.of(Weekdays.Tuesday); // LINE C
        System.out.println("set when added tuesday : " + set);
    
        EnumSet weekends = EnumSet.of(Weekdays.Saturday, Weekdays.Sunday); // LINE D
        System.out.println("weekends : " + weekends);
    
        EnumSet otherweekdays = EnumSet.complementOf(weekends); // LINE E
        System.out.println("Otherdays : " + otherweekdays);
    
        set = EnumSet.allOf(Weekdays.class); // LINE F
        EnumSet setCopy = EnumSet.copyOf(set); // LINE G
        System.out.println("setCopy elements : " + setCopy);
    }
}
OUTPUT

null
set when added monday : [Monday]
set when added tuesday : [Tuesday]
weekends : [Saturday, Sunday]
Otherdays : [Monday, Tuesday, Wednesday, Thursday, Friday]
setCopy elements : [Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday]

DESCRIPTION

In the above program we demonstrated how EnumSet works. At LINE A we created a null EnumSet and displayed it. At LINE B we added Monday to set using Of method and displayed. At LINE C we added Tuesday to set using the same Of method which replaced Monday in the set. At LINE D we created weekends a EnumSet and added Saturday, Sunday and displayed it. At LINE E we created otherweekdays which don't have the weekends using complementOf method and then added all the elements in the enum Weekdays to set using allOf method at LINE F and created a copy of set as setCopy using copyOf method at LINE G and displayed it.

THINGS TO TRY
  • Add another Birthday to set using add method. Use the below sample code
    set.add("Birthday");
    The above code throws java.lang.ClassCastException: since the elements in the EnumSet must come from a single enum type.
  • Check whether set and setCopy are equal using == operator and equals method. Use the below sample code.
    System.out.println(set == setCopy);
    The output for the above code is false since set and setCopy are not referring to Set.
    System.out.println(set.equals(setCopy));
    The output for the above code is true since set and setCopy contains same elements.

0
Wrong
Score more than 2 points

© meritcampus 2016 - 2017

All Rights Reserved.