Write a Java program to Remove all elements of EnumSet collection


This Java program demonstrates the usage of the clear method provided by the EnumSet class to remove all elements from an EnumSet object. The program creates an empty EnumSet object named enum_set that can hold the constants of the WEEKDAY enum using the noneOf method. Then, it adds some of the constants of the WEEKDAY enum to the enum_set object using the add method. The program prints the enum_set object before removing all elements using the clear method. Finally, it removes all elements from the enum_set object using the clear method and prints the enum_set object again to confirm that all elements have been removed.

Source Code

import java.util.*;
public class RemoveAll
{
	public static void main(String[] args)
	{
		EnumSet < WEEKDAY > enum_set = EnumSet.noneOf(WEEKDAY.class);
 
		enum_set.add(WEEKDAY.Monday);
		enum_set.add(WEEKDAY.Wednesday);
		enum_set.add(WEEKDAY.Friday);
		enum_set.add(WEEKDAY.Thursday);
 
		System.out.println("Before Remove all Elements of enumSet is: " + enum_set);
 
		enum_set.clear();
		System.out.println("After Remove all Elements of enumSet is: " + enum_set);
	}
}
enum WEEKDAY {
	Monday,
	Tuesday,
	Wednesday,
	Thursday,
	Friday,
	Saturday,
	Sunday 
};

Output

Before Remove all Elements of enumSet is: [Monday, Wednesday, Thursday, Friday]
After Remove all Elements of enumSet is: []