Java EnumSet
Syntax:
java.lang.Object
java.util.AbstractCollection
java.util.AbstractSet
java.util.EnumSet
How to create an empty EnumSet and add some elements?
//write this code after import libray
enum day
{
SUN, MON, TUE, WED, THU, FRI, SAT
};
//write this code inside class
EnumSet set1;
set1 = EnumSet.of(day.SUN, day.MON, day.TUE, day.WED);
How to create complement set?
EnumSet complementOf(EnumSet s): 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.
set2;
set2 = EnumSet.complementOf(set1);
How to insert all elements?
EnumSet allOf(Class elementType): Creates an enum set containing all of the elements in the specified element type.
set3;
set3 = EnumSet.allOf(day.class);
How to insert range of elements?
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.
set4:
set4 = EnumSet.range(day.MON, day.THU);
How to Check if the collection contains a specific element?
How to remove simply remove elements?
Syntax:
java.lang.Object
java.util.AbstractCollection
java.util.AbstractSet
java.util.EnumSet
- The EnumSet collection from the java.util package.
- An EnumSet is a specialized Set collection to work with enum classes.
- The EnumSet is one of the specialized implementation of Set interface for use with the enumeration type.
- The EnumSet class is a member of the Java Collections Framework & is not synchronized.
- It can contain only enum values and all the values have to belong to the same enum.
- It doesn't allow to add null values, throwing a NullPointerException in an attempt to do so.
- It’s a high performance set implementation, much faster than HashSet.
How to create an empty EnumSet and add some elements?
- EnumSet of(E e1): Creates an enum set initially containing the specified elements.
- EnumSet of(E e11, E el2): Creates an enum set initially containing the specified elements.
- EnumSet of(E e11, E el2, Eel3): Creates an enum set initially containing the specified elements.
- EnumSet of(E e11, E el2, Eel3,E el4….): Creates an enum set initially containing the specified elements.
- EnumSet of(E e1, E rest…): Creates an enum set initially containing the specified elements.
//write this code after import libray
enum day
{
SUN, MON, TUE, WED, THU, FRI, SAT
};
//write this code inside class
EnumSet
set1 = EnumSet.of(day.SUN, day.MON, day.TUE, day.WED);
How to create complement set?
EnumSet complementOf(EnumSet s): 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.
- Example:
set2 = EnumSet.complementOf(set1);
How to insert all elements?
EnumSet allOf(Class elementType): Creates an enum set containing all of the elements in the specified element type.
- Example:
set3 = EnumSet.allOf(day.class);
How to insert range of elements?
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.
- Example:
set4 = EnumSet.range(day.MON, day.THU);
How to Check if the collection contains a specific element?
- Example: set1.contains(day.MON);
- Example:
How to remove simply remove elements?
- Example: set1.remove(day.TUE);
- EnumSet copyof(): The method is used to copy all of the contents from a collection to a new enum set.
- EnumSet copyof(EnumSet s): The method is used to copy all of the contents from an existing EnumSet to a new enum set.
- EnumSet clone(): The method is used to return a shallow copy of the existing or this set.
- EnumSet noneOf(): The method is used to create a null set.
Tags:
Java