EnumSet in java with program



In this Collection framework tutorial we will learn what is java.util.EnumSet in Collection framework in java.



Contents of page :


1) What is hierarchy of EnumSet in java?
-java.lang.Object
-java.util.AbstractCollection
 -java.util.AbstractSet
  -java.util.EnumSet


For more detailed hierarchy information read : Set hierarchy in java


2) java.util.EnumSet
A EnumSet is specialized Set implementation for use with enum types in java.
EnumSet all elements comes from a single enum type that is specified when the set is created in java.


3) Order of elements in EnumSet in java
The java.util.EnumSet maintains natural order (the order in which the enum constants are declared) of elements in java.


4) Iterator on EnumSet in java
The iterator returned by the iterator method traverses the elements in their natural order (the order in which the enum constants are declared).
iterator never throw ConcurrentModificationException and it may or may not show the effects of any modifications to the set that occur while the iteration is in progress.


5) Null elements in EnumSet in java
Null elements are not allowed in EnumSet in java. Attempts to insert a null element will throw NullPointerException in java.


6) Example/ Program to demonstrate java.util.EnumSet in java
import java.util.EnumSet;
import java.util.Set;
import java.util.Collections;


 
/**
* @author AnkitMittal
*/
public class EnumSetExample {
   /** enum */
   private enum Days {
          Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday;
          // allDays will contain days declared in enum above.
          public static Set<Days> allDays = EnumSet.allOf(Days.class);
         
          //weekDays will contain days between Monday to Friday.
   public static Set<Days> weekDays = EnumSet.range(Monday, Friday);
  
   //Method will return true current day is weekDay
   public boolean isWeekDay() {
     return weekDays.contains(this);
   }
  
   }
   /** Main */
   public static final void main(final String args[]) {
          System.out.println("\n--------0. Print days enum -----------");
          System.out.println(Days.allDays);
          System.out.println("days in allDays = "+Days.allDays.size());
  
         
          System.out.println("\n--------1. iterate over allDays -----------");
          //iterate over allDays
          for (Days day : Days.allDays) {
                 System.out.println("Day-" + (day.ordinal() + 1) + " " + day);
          }
         
         
          System.out.println("\n--------2. weekDay or weekEnd -----------");
          /**Check whether passed day is weekDay or weekEnd*/
         
          //Pass Monday   And   Check whether Monday is weekDay or weekEnd
          Days day=Days.Monday;
          System.out.println(day+ (day.isWeekDay() ? " is WeekDay" : " is weekEnd"));
         
          //Pass Sunday   And   Check whether Sunday is weekDay or weekEnd
          day=Days.Sunday;
          System.out.println(day+ (day.isWeekDay() ? " is WeekDay" : " is weekEnd"));
         
         
         
   System.out.println("\n----3. contains --------");
   day=Days.Monday;
   System.out.println("allDays contains Monday : "+Days.allDays.contains(day));
   System.out.println("\n----4. ordinal --------");
   day=Days.Monday;
   System.out.println("ordinal of Monday : "+ day.ordinal());
         
         
          System.out.println("\n-------5. Find number of days in weekEnd ---------");
   /**find weekEnd days by removing weekdays from allDays */
         
          //weeEnd currently consists of allDays
          Set<Days> weekEnd = Days.allDays;
         
          //Now, remove weekDays from allDays
   weekEnd.removeAll(Days.weekDays);
  
   //Currently weeEnd consists of Saturday and Sunday
   System.out.println("days in weekEnd = "+weekEnd);
   System.out.println("The weekEnd is "+weekEnd.size()+" days long");
          
          System.out.println("\n-------6. synchronizing EnumSet ---------");
   Set<Days> s = Collections.synchronizedSet(EnumSet.noneOf(Days.class));
   }
}
/*OUTPUT
--------0. Print days enum -----------
[Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday]
days in allDays = 7
--------1. iterate over allDays -----------
Day-1 Monday
Day-2 Tuesday
Day-3 Wednesday
Day-4 Thursday
Day-5 Friday
Day-6 Saturday
Day-7 Sunday
--------2. weekDay or weekEnd -----------
Monday is WeekDay
Sunday is weekEnd
----3. contains --------
allDays contains Monday : true
----4. ordinal --------
ordinal of Monday : 0
-------5. Find number of days in weekEnd ---------
days in weekEnd = [Saturday, Sunday]
The weekEnd is 2 days long
-------6. synchronizing EnumSet ---------

*/


7) synchronizing EnumSet in java
java.util.EnumSet is not synchronized, we can synchronize it using synchronizedSet method of Collections class in java.
Set<Days> s = Collections.synchronizedSet(EnumSet.noneOf(Days.class));
Days enum have been used in above program in java.


8) Performance of EnumSet in java
This representation is extremely compact and efficient in java. The space and time performance of this class should be good enough to allow its use as a high-quality, type safe alternative to traditional int-based "bit flags." Even bulk operations such as containsAll and retainAll run very quickly if their argument is also an enum set in java.


So in this Collection framework tutorial we learned what is java.util.EnumSet in Collection framework in java.


/** JavaMadeSoEasy.com */



Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.


RELATED LINKS>

Collection in java



Set >





List vs Set - Similarity and Differences






HashSet - isEmpty, size and clear methods program in java


HashSet - iterate using iterator, Enumeration and enhanced for loop program in java


HashSet - fail-safe or fail-fast iteration using iterator, Enumeration and enhanced for loop program in java


HashSet - synchronizing using Collections.synchronizedSet program in java


HashSet - making set unmodifiable using Collections.unmodifiableSet in java


Important Similarity and Differences Collection classes in concurrent and non-concurrent packages >


TreeSet vs ConcurrentSkipListSet - Similarity and Differences with program in java


eEdit
Must read for you :