Java Program to Create an Enumset Collection From All Elements of an Enum

You are currently viewing Java Program to Create an Enumset Collection From All Elements of an Enum

Java program to create an EnumSet collection from all elements of an enum

// Java program to create an EnumSet collection 
// from all elements of an enum

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;

    //Adding elements to EnumSet.
    enumSet = EnumSet.allOf(COLORS.class);

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

Output

EnumSet is: [RED, GREEN, BLUE, BLACK, WHITE]

Ranjith

Hi, I'm Manoj a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply