Java HashMap.clone()

Syntax

HashMap.clone() has the following syntax.

public Object clone()

Example

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


/*from  w ww .  ja va2s  . c  om*/
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, "java2s.com");
      
      // clone 1st map
      newmap2=(HashMap)newmap1.clone();
      
      System.out.println("1st Map: " + newmap1);
      System.out.println("Cloned 2nd Map: " + newmap2);   
   }    
}

The code above generates the following result.