Write a Java program to add elements to EnumSet collection using add() method


This Java code demonstrates how to add elements to an EnumSet using the add() method. The code first creates an empty EnumSet of type WEEKDAY using the noneOf() method. Next, the code adds two elements, WEEKDAY.Wednesday and WEEKDAY.Saturday, to the EnumSet using the add() method.

Finally, the code prints the contents of the EnumSet using System.out.println(). This will print the two elements added to the EnumSet, which are Wednesday and Saturday. The WEEKDAY enum defines seven values corresponding to the days of the week.

Source Code

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

Output

EnumSet Elements : [Wednesday, Saturday]