The Java.util.EnumSet package is required to create an EnumSet, so you will have to import it first in your Java program. Unlike other set implementations, EnumSet does not have a public constructor, so you must use the predefined methods. These predefined methods include: allOf (Size)- Using the allof() method, you can create an enum set containing all values of the specified enum type Size. noneOf(Size)- An empty EnumSet is created with the noneOf() method. range(e1,e2) Method- Range() creates an enum set which includes both values for an enum between e1 and e2 within a given range. of() Method- Using the () method, an enum set containing the specified element is created. add(): this method inserts the specified values into the EnumSet addAll(): this method inserts the specified collections to the set iterator(): this method is used to access the Enum set elements hasNext(): this method returns true if there is a next element in the EnumSet next(): this method returns the next elements in the EnumSet remove(): this method removes the specified element from the EnumSet removeAll(): this method removes all elements from the EnumSet Example of EnumSet import java.util.EnumSet; import java.util.Iterator; public class EnumExample { enum ES { a, b, c, d; } public static void main(String[] args) { // Creating EnumSet EnumSet<ES> sizes = EnumSet.allOf(ES.class); Iterator<ES> iterate = sizes.iterator(); System.out.print("EnumSet Values: "); while(iterate.hasNext()) { System.out.print(iterate.next()); System.out.print(", "); } } } Output: EnumSet Values: a, b, c, d
The Methods of EnumSet
Article Contributed By :
|
|
|
|
87 Views |