An EnumSet is a set of all of the elements that are closely related to each other that is they are from a single enum type that may defined, explicitly or implicitly, at the creation time of set. EnumSet class extends the AbstractSet class and implements the Set interface. It is specifically for use with enum type. Internally bit vector representation of Enum sets are highly compact and effective. The performance of the space and time of this class should be good to give the permission of its uses as a high quality, traditionalistic typesafe option to int-based "bit-flags". A type-safe enumerations can be applied in various modes based on their features they include : A list of Values, for iterating through all the values of enumeration. implementing Cloneable. implementing Serializable.
Java Collection Framework - EnumSet class
An EnumSet is a set of all of the elements that are closely related to each other that is they are from a single enum type that may defined, explicitly or implicitly, at the creation time of set. EnumSet class extends the AbstractSet class and implements the Set interface. It is specifically for use with enum type. Internally bit vector representation of Enum sets are highly compact and effective. The performance of the space and time of this class should be good to give the permission of its uses as a high quality, traditionalistic typesafe option to int-based "bit-flags". A type-safe enumerations can be applied in various modes based on their features they include :
- A list of Values, for iterating through all the values of enumeration.
- implementing Cloneable.
- implementing Serializable.
In an EnumSet 'null' elements are not allowed, if you will be try to insert a null element then it will display a 'NullPointerException'. As the other most collection execution it is not synchronized. If an enum set is accessed by the multiple threads at the same time and there is at least one thread alters the set, it should be synchronized externally. This is generally achieved by synchronized to few object that normally encapsulates the enum set. To keep away from an unexpected unsynchronized access, there should be "wrapped" the set using Collections.synchronizedSet(java.util.Set) at the time of creation if there is no such object is existed.
Syntax
public abstract class EnumSet<E extends Enum<E>>
Parameter description
E : It is the type of enum set.
Example :
package devmanuals.com; import java.util.EnumSet; public final class EnumSetDemo { private enum Workingday { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday; public static final EnumSet<Workingday> Workdays = EnumSet.range( Monday, Friday); public final boolean isWorkday() { return Workdays.contains(this); } public static final EnumSet<Workingday> WHOLE_WEEK = EnumSet.allOf(Workingday.class); } public static final void main(final String args[]) { System.out.println("Workday Schedule:"); for (final Workingday workingday : Workingday.WHOLE_WEEK) System.out.println(String.format("%d. On %s you "+ (workingday.isWorkday() ? "have to work" : "can relax")+ ".", workingday.ordinal() + 1, workingday)); System.out.println("Do I have to work the whole week?"); System.out.println(Workingday.Workdays.containsAll(Workingday.WHOLE_WEEK) ? "Yes, unfortunately.": "Certainly not."); final EnumSet<Workingday> weekend = Workingday.WHOLE_WEEK.clone(); weekend.removeAll(Workingday.Workdays); System.out.println(String.format("The weekend is %d days long.", weekend.size())); } }
Output :
Workday Schedule: 1. On Monday you have to work. 2. On Tuesday you have to work. 3. On Wednesday you have to work. 4. On Thursday you have to work. 5. On Friday you have to work. 6. On Saturday you can relax. 7. On Sunday you can relax. Do I have to work the whole week? Certainly not. The weekend is 2 days long. |
[ 0 ] Comments