Java Program to Remove a Specified Element From Enumset Collection

Java program to remove a specified element from EnumSet collection

// Java program to remove a specified element 
// from the EnumSet collection

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("Elements of enumSet before removing element: " + enumSet);

    enumSet.remove(COLORS.GREEN);

    System.out.println("Elements of enumSet after removing element: " + enumSet);
  }
}

Output

Elements of enumSet before removing element: [RED, GREEN, BLUE]
Elements of enumSet after removing element: [RED, 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