Java.util.EnumMap.clone() Method


Advertisements


Description

The java.util.EnumMap.clone() method returns a copy of this enum map.

Declaration

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

public EnumMap<K,V> clone()

Parameters

  • NA

Return Value

This method returns a shallow copy of this enum map.

Exception

  • NA

Example

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

package com.tutorialspoint;

import java.util.EnumMap;

public class EnumMapDemo {

   // create an enum
   public enum Numbers {

   ONE, TWO, THREE, FOUR, FIVE
   };

   public static void main(String[] args) {
   EnumMap<Numbers, String> map1 =
   new EnumMap<Numbers, String>(Numbers.class);
   EnumMap<Numbers, String> map2 =
   new EnumMap<Numbers, String>(Numbers.class);

   // associate values in map1
   map1.put(Numbers.ONE, "1");
   map1.put(Numbers.TWO, "2");
   map1.put(Numbers.THREE, "3");
   map1.put(Numbers.FOUR, "4");

   // print the whole map
   System.out.println("map1:" + map1);

   // clone map1 to map2
   map2 = map1.clone();

   // print map2
   System.out.println("map2:" + map2);

   }
}

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

map1:{ONE=1, TWO=2, THREE=3, FOUR=4}
map2:{ONE=1, TWO=2, THREE=3, FOUR=4}

java_util_enummap.htm

Advertisements