Write a Java program to create a clone of an EnumSet collection


This Java program demonstrates the cloning of an EnumSet using the clone() method provided by the EnumSet class.

The program creates two EnumSet objects named enum_set and clone_set, both of which can hold the constants of the WEEKDAY enum. The enum_set is initialized with all the constants of the WEEKDAY enum using the allOf method of the EnumSet class. The program then prints the contents of the enum_set.

After that, the clone_set is created as a clone of the enum_set using the clone() method. The contents of the clone_set are then printed.

Source Code

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

Output

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