×

Java Programs

Java Practice

Java program to create an EnumSet using the range() method

Java example to create an EnumSet using the range() method.
Submitted by Nidhi, on May 28, 2022

Problem statement

In this program, we will create an Enum for COLORS constants. Then we will create an EnumSet collection using the range() method from an enum.

Java program to create an EnumSet using the range() method

The source code to create an EnumSet using the range() method is given below. The given program is compiled and executed successfully.

// Java program to create an EnumSet 
// using range() method

import java.util.*;

//Enum for color constants
enum COLORS {
  RED,
  GREEN,
  BLUE,
  BLACK,
  WHITE
};

public class Main {
  public static void main(String[] args) {
    EnumSet < COLORS > enumSet;

    enumSet = EnumSet.range(COLORS.GREEN, COLORS.BLACK);

    System.out.println("EnumSet is: " + enumSet);
  }
}

Output

EnumSet is: [GREEN, BLUE, BLACK]

Explanation

The Main class contains a main() method. The main() method is the entry point for the program. And, created a reference of the EnumSet collection and initialized it with elements of COLORS elements using the range() method and printed the result.

Java EnumSet Programs »





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.