Java EnumSet - range() Method
The java.util.EnumSet.range() method is used to create an enum set initially containing all of the elements in the range defined by the two specified endpoints.
Syntax
public static <E extends Enum<E>> EnumSet<E> range(E from, E to)
Here, E is the type of element maintained by the container.
Parameters
from |
Specify the first element in the range. |
to |
Specify the last element in the range. |
Return Value
Returns an enum set initially containing all of the elements in the range defined by the two specified endpoints.
Exception
- Throws NullPointerException, if from or to are null.
- Throws IllegalArgumentException, if from.compareTo(to) > 0.
Example:
In the example below, the java.util.EnumSet.range() method is used to create an enum set initially containing all of the elements in the specified range.
import java.util.*; public class MyClass { //creating an enum public enum weekday{ SUN, MON, TUE, WED, THU, FRI, SAT } public static void main(String[] args) { //creating an EnumSet EnumSet<weekday> Set; //Add elements using range() method Set = EnumSet.range(weekday.MON, weekday.FRI); //print the content of Set System.out.println("Set contains: "+ Set); } }
The output of the above code will be:
Set contains: [MON, TUE, WED, THU, FRI]
❮ Java.util - EnumSet