Write a Java program to create an EnumSet collection


This Java program demonstrates the usage of EnumSet, a specialized Set implementation in the Java Collections Framework used to hold a set of enum constants.

The program creates an EnumSet object named enum_set which can hold the constants of the WEEKDAY enum. Then, it adds three elements to the enum_set using the of method of the EnumSet class. Finally, the program prints the contents of the enum_set using the println method.

Source Code

public class Create_EnumSet
{
	public static void main(String[] args)
	{
		EnumSet < WEEKDAY > enum_set;
		//Adding elements to enum_set
		enum_set = EnumSet.of(WEEKDAY.Wednesday, WEEKDAY.Friday, WEEKDAY.Sunday);
		System.out.println("EnumSet is : " + enum_set);
	}
}
enum WEEKDAY {
	Monday,
	Tuesday,
	Wednesday,
	Thursday,
	Friday,
	Saturday,
	Sunday 
};

Output

EnumSet is : [Wednesday, Friday, Sunday]