EnumSet range() Method in Java

The java.util.EnumSet.range(E start_point, E end_point) method in Java is used to create an enum set with the elements defined by the specified range in the parameters.

Syntax:

Enum_set = EnumSet.range(E start_point, E end_point)

Parameters: The method accepts two parameters of the object type of enum:



  • start_point: This refers to the starting element that is needed to be added to the enum set.
  • end_point: This refers to the last element which is needed to be added to the enum set.

Return Value: The method returns the enum set created by the elements mentioned within the specified range.

Exceptions: The method throws two types of exception:

  • NullPointerException is thrown if any of the starting or the last element is NULL.
  • IllegalArgumentException is thrown when the first element is greater than the last element with respect to the position.

Below programs illustrate the use of range() method:
Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate range() method
import java.util.*;
  
// Creating an enum of GFG type
enum GFG {
    Welcome,
    To,
    The,
    World,
    of,
    Geeks
}
;
  
public class Enum_Set_Demo {
  
    public static void main(String[] args)
    {
  
        // Creating an EnumSet
        EnumSet<GFG> e_set;
  
        // Input the values using range()
        e_set = EnumSet.range(GFG.The, GFG.Geeks);
  
        // Displaying the new set
        System.out.println("The enum set is: " + e_set);
    }
}

chevron_right


Output:

The enum set is: [The, World, of, Geeks]

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to demonstrate range() method
import java.util.*;
  
// Creating an enum of CARS type
enum CARS {
    RANGE_ROVER,
    MUSTANG,
    CAMARO,
    AUDI,
    BMW
}
;
  
public class Enum_Set_Demo {
  
    public static void main(String[] args)
    {
  
        // Creating an EnumSet
        EnumSet<CARS> e_set;
  
        // Input the values using range()
        e_set = EnumSet.range(CARS.RANGE_ROVER, CARS.CAMARO);
  
        // Displaying the new set
        System.out.println("The enum set is: " + e_set);
    }
}

chevron_right


Output:

The enum set is: [RANGE_ROVER, MUSTANG, CAMARO]


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.