Write a Java program to Remove a specified element from EnumSet collection


This Java code demonstrates how to remove a specified element from an EnumSet using the remove() method.

The program first creates an empty EnumSet of type WEEKDAY using the noneOf() method. Then, it adds three weekday constants Monday, Wednesday, and Friday to the set using the add() method.

Next, the program prints the set before removing an element using System.out.println(). Then, it removes the Monday element from the set using the remove() method. Finally, it prints the set again to show the updated set.

Source Code

import java.util.*;
public class Remove_Specified
{
	public static void main(String[] args)
	{
		EnumSet < WEEKDAY > item = EnumSet.noneOf(WEEKDAY.class);
 
		item.add(WEEKDAY.Monday);
		item.add(WEEKDAY.Wednesday);
		item.add(WEEKDAY.Friday);
		System.out.println("Before Removing Element : " + item);
 
		item.remove(WEEKDAY.Monday);
		System.out.println("After Removing Element : " + item);
	}
}
enum WEEKDAY {
	Monday,
	Tuesday,
	Wednesday,
	Thursday,
	Friday,
	Saturday,
	Sunday 
};

Output

Before Removing Element : [Monday, Wednesday, Friday]
After Removing Element : [Wednesday, Friday]