Home »
Java »
Java Programs
Java program to remove a specified element from EnumSet collection
Given an EnumSet collection, we have to remove a specified element from it.
Submitted by Nidhi, on May 29, 2022
Problem Solution:
In this program, we will create an Enum for COLORS constants. Then we will create an EnumSet collection and add elements to it. After that, we will remove a specified element using the remove() method.
Program/Source Code:
The source code to remove a specified element from the EnumSet collection is given below. The given program is compiled and executed successfully.
// 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]
Explanation:
The Main class contains a main() method. The main() method is the entry point for the program. And, created an EnumSet collection and add elements to it. Then we removed a specified item from created EnumSet collection and printed the updated EnumSet Collection.
Java EnumSet Programs »