Java Collection Tutorial - Java HashMap.clone()


HashMap.clone() has the following syntax.

public Object clone()


In the following code shows how to use HashMap.clone() method.

/*from  w  w w.  java2s .  c  o m*/
import java.util.HashMap;

public class Main {
   public static void main(String args[]) {
      // create two hash maps

      HashMap<Integer, String> newmap1 = new HashMap<Integer, String>();      
      HashMap<Integer, String> newmap2 = new HashMap<Integer, String>();      
      // populate hash map
      newmap1.put(1, "tutorials");
      newmap1.put(2, "from");
      newmap1.put(3, "");
      // clone 1st map
      System.out.println("1st Map: " + newmap1);
      System.out.println("Cloned 2nd Map: " + newmap2);   

The code above generates the following result.