Java EnumMap - clone() Method
The java.util.EnumMap.clone() method returns a shallow copy of this EnumMap. The values themselves are not cloned.
Syntax
public EnumMap<K,V> clone()
Here, K and V are the type of key and value respectively maintained by the container.
Parameters
No parameter is required.
Return Value
Returns a shallow copy of this EnumMap.
Exception
NA.
Example:
In the example below, the java.util.EnumMap.clone() method returns a shallow copy of the given EnumMap.
import java.util.*; public class MyClass { //creating an enum public enum weekday{ MON, TUE, WED, THU, FRI } public static void main(String[] args) { //creating EnumMaps EnumMap<weekday,Integer> MyMap1 = new EnumMap<weekday,Integer>(weekday.class); EnumMap<weekday,Integer> MyMap2 = new EnumMap<weekday,Integer>(weekday.class); //associate values in MyMap1 MyMap1.put(weekday.MON, 1); MyMap1.put(weekday.TUE, 2); MyMap1.put(weekday.WED, 3); MyMap1.put(weekday.THU, 4); MyMap1.put(weekday.FRI, 5); //printing MyMap1 System.out.println("MyMap1 contains: " + MyMap1); //create a copy of MyMap1 into MyMap2 MyMap2 = MyMap1.clone(); //printing MyMap2 System.out.println("MyMap2 contains: " + MyMap2); } }
The output of the above code will be:
MyMap1 contains: {MON=1, TUE=2, WED=3, THU=4, FRI=5} MyMap2 contains: {MON=1, TUE=2, WED=3, THU=4, FRI=5}
❮ Java.util - EnumMap