Java Utility Library

Java EnumSet - clone() Method



The java.util.EnumSet.clone() method returns a copy of the given set.

Syntax

public EnumSet<E> clone()

Here, E is the type of element maintained by the container.

Parameters

No parameter is required.

Return Value

Returns a copy of this set.

Exception

NA.

Example:

In the example below, the java.util.EnumSet.clone() method returns a copy of the given set.

import java.util.*;

public class MyClass {
  
  //creating an enum
  public enum weekday{
    SUN, MON, TUE, WED, THU, FRI, SAT
  }

  public static void main(String[] args) {
    //creating an EnumSet from enum weekday
    EnumSet<weekday> Set1 = EnumSet.allOf(weekday.class);

    //print the content of Set1
    System.out.println("Set1 contains: "+ Set1);  

    //creating a copy of Set1 into Set2
    EnumSet<weekday> Set2 = Set1.clone();

    //print the content of Set2
    System.out.println("Set2 contains: "+ Set2);     
  }
}

The output of the above code will be:

Set1 contains: [SUN, MON, TUE, WED, THU, FRI, SAT]
Set2 contains: [SUN, MON, TUE, WED, THU, FRI, SAT]

❮ Java.util - EnumSet