A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/java/java-enumset-range-method/ below:

Java EnumSet range() Method - GeeksforGeeks

Java EnumSet range() Method

Last Updated : 22 May, 2025

The EnumSet.range() method is a part of the java.util package. This method is used to create an EnumSet that contains all enum constants between the specified start and end points, and it should be inclusive. It easily creates a subset of enum constants within a specified range.

Syntax of EnumSet range() Method

EnumSet<E> range(E start_point, E end_point)

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

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

Exceptions: The method throws two types of exceptions:

Examples of Java EnumSet range() Method

Example 1: In this example, we will create an EnumSet using the range() method with the GFG enum.

Java
// Java program to demonstrate
// EnumSet.range() method usage
import java.util.*;

// Define an enum type
enum GFG {
    Welcome, To, The, World, of, Geeks
}

public class Geeks {
    public static void main(String[] args) {
        
        // Create an EnumSet with enum constants
        // ranging from The to Geeks inclusive
        EnumSet<GFG> es = EnumSet.range(GFG.The, GFG.Geeks);
        
        // Display the EnumSet created with the range
        System.out.println("The enum set is: " + es);
    }
}

Example 2: In this example, we will use the range() method with a different enum called CARS.

Java
// Java program to demonstrate EnumSet.range()
// method with a different enum
import java.util.*;

// Define another enum type
enum CARS {
    RANGE_ROVER, MUSTANG, CAMARO, AUDI, BMW
}

public class Geeks {
    public static void main(String[] args) {
        
        // Create an EnumSet with enum constants
        // ranging from RANGE_ROVER to CAMARO inclusive
        EnumSet<CARS> es 
        = EnumSet.range(CARS.RANGE_ROVER, CARS.CAMARO);
        
        // Display the EnumSet created with the range
        System.out.println("The enum set is: " + es);
    }
}

Important Points:



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4