Java Program to Add Elements to Enumset Collection Using Add() Method

Java program to add elements to EnumSet collection using add() method

// Java program to add elements to EnumSet 
// using add() 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.noneOf(COLORS.class);

    enumSet.add(COLORS.RED);
    enumSet.add(COLORS.GREEN);
    enumSet.add(COLORS.BLUE);

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

Output

enumSet elements: [RED, GREEN, BLUE]
Share on:

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

Leave a Comment