Write a Java program to get the size of the EnumSet collection


This Java program demonstrates the usage of the size method provided by the EnumSet class to get the number of elements in 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. Finally, the program prints the number of elements in the enum_set object using the size method.

Source Code

import java.util.*;
public class GetSize
{
	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("Size of EnumSet : " + enum_set.size());
	}
}
enum WEEKDAY {
	Monday,
	Tuesday,
	Wednesday,
	Thursday,
	Friday,
	Saturday,
	Sunday 
};

Output

Size of EnumSet : 4