Java.util.EnumSet.clone() Method

Advertisements


Description

The java.util.EnumSet.clone() method Returns a copy of this set.

Declaration

Following is the declaration for java.util.EnumSet.clone() method

public EnumSet<E> clone()

Parameters

  • NA

Return Value

This method returns a copy of this set.

Exception

  • NA

Example

The following example shows the usage of java.util.EnumSet.clone() method.

package com.tutorialspoint;

import java.util.*;

public class EnumSetDemo {

   // create an enum
   public enum Numbers {

      ONE, TWO, THREE, FOUR, FIVE
   };

   public static void main(String[] args) {

      // create an EnumSet that has all elements from Numbers 
      EnumSet<Numbers> set1 = EnumSet.allOf(Numbers.class);

      // print the set
      System.out.println("Set1:" + set1);

      // create a set2 which is a copy of set1
      EnumSet<Numbers> set2 = set1.clone();

      // print the updated set
      System.out.println("Set2:" + set2);

   }
}

Let us compile and run the above program, this will produce the following result:

Set1:[ONE, TWO, THREE, FOUR, FIVE]
Set2:[ONE, TWO, THREE, FOUR, FIVE]



Advertisements
Advertisements