Write a Java program to create an EnumSet collection from all elements of an enum


This Java program demonstrates the usage of the allOf method provided by the EnumSet class to create an EnumSet object containing all the constants of the WEEKDAY enum. The program simply creates an EnumSet object named enum_set which can hold the constants of the WEEKDAY enum. Then, it initializes the enum_set with all the constants of the WEEKDAY enum using the allOf method of the EnumSet class. Finally, the program prints the contents of the enum_set using the println method.

Source Code

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

Output

EnumSet  : [Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday]