Home »
Java »
Java Programs
Java program to create a clone of an EnumSet collection
Given an EnumSet collection, we have to create a clone of it.
Submitted by Nidhi, on May 27, 2022
Problem statement
In this program, we will create an Enum for COLORS constants. Then we will create an EnumSet and get the clone of the EnumSet collection using the clone() method.
Java program to create a clone of an EnumSet collection
The source code to create a clone of an EnumSet collection is given below. The given program is compiled and executed successfully.
// Java program to create a clone of an
// EnumSet collection
import java.util.*;
//Enum for color constants
enum COLORS {
RED,
GREEN,
BLUE,
BLACK,
WHITE
};
public class Main {
public static void main(String[] args) {
EnumSet < COLORS > enumSet;
EnumSet < COLORS > clone;
//Adding elements to EnumSet.
enumSet = EnumSet.allOf(COLORS.class);
System.out.println("EnumSet is: " + enumSet);
clone = enumSet.clone();
System.out.println("Clone of enumSet is: " + clone);
}
}
Output
EnumSet is: [RED, GREEN, BLUE, BLACK, WHITE]
Clone of enumSet is: [RED, GREEN, BLUE, BLACK, WHITE]
Explanation
The Main class contains a main() method. The main() method is the entry point for the program. And, created a reference of the EnumSet collection and initialized it with all elements of COLORS elements using the allOf() method. After that, we created the clone of enumSet using the clone() method and printed the result.
Java EnumSet Programs »