Java HashMap clone() Method



Description

The Java HashMap clone() method is used to return a shallow copy of this HashMap instance.

Declaration

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

public Object clone()

Parameters

NA

Return Value

The method call returns a shallow copy of this map.

Exception

NA

Clear a HashMap of Integer, Integer Pair Example

The following example shows the usage of Java HashMap clone() method to get a shallow copy of a Map. We've created two Map objects of Integer,Integer pair. Then few entries are added to one map, another map is populated using clone() method and then both maps are printed.

package com.tutorialspoint;

import java.util.HashMap;

public class HashMapDemo {
   public static void main(String args[]) {
      
      // create two hash maps
      HashMap<Integer, Integer> newmap1 = new HashMap<>();
      HashMap<Integer, Integer> newmap2 = new HashMap<>();

      // populate 1st map
      newmap1.put(1, 1);
      newmap1.put(2, 2);
      newmap1.put(3, 3); 

      // clone 1st map
      newmap2 = (HashMap)newmap1.clone();

      System.out.println("1st Map: " + newmap1);
      System.out.println("Cloned 2nd Map: " + newmap2);   
   }
}

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

1st Map: {1=1, 2=2, 3=3}
Cloned 2nd Map: {1=1, 2=2, 3=3}

Clear a HashMap of Integer, String Pair Example

The following example shows the usage of Java HashMap clone() method to get a shallow copy of a Map. We've created two Map objects of Integer,String. Then few entries are added to one map, another map is populated using clone() method and then both maps are printed.

package com.tutorialspoint;

import java.util.HashMap;

public class HashMapDemo {
   public static void main(String args[]) {
      
      // create two hash maps
      HashMap<Integer, String> newmap1 = new HashMap<>();
      HashMap<Integer, String> newmap2 = new HashMap<>();

      // populate 1st map
      newmap1.put(1, "A");
      newmap1.put(2, "B");
      newmap1.put(3, "C"); 

      // clone 1st map
      newmap2 = (HashMap)newmap1.clone();

      System.out.println("1st Map: " + newmap1);
      System.out.println("Cloned 2nd Map: " + newmap2);   
   }
}

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

1st Map: {1=A, 2=B, 3=C}
Cloned 2nd Map: {1=A, 2=B, 3=C}

Clear a HashMap of Integer, Student Pair Example

The following example shows the usage of Java HashMap clone() method to get a shallow copy of a Map. We've created two Map objects of Integer,Student pair. Then few entries are added to one map, another map is populated using clone() method and then both maps are printed.

package com.tutorialspoint;

import java.util.HashMap;

public class HashMapDemo {
   public static void main(String args[]) {
      
      // create two hash maps
      HashMap<Integer, Student> newmap1 = new HashMap<>();
      HashMap<Integer, Student> newmap2 = new HashMap<>();

      // populate hash map
      newmap1.put(1, new Student(1, "Julie"));
      newmap1.put(2, new Student(2, "Robert"));
      newmap1.put(3, new Student(3, "Adam"));

      // clone 1st map
      newmap2 = (HashMap)newmap1.clone();

      System.out.println("1st Map: " + newmap1);
      System.out.println("Cloned 2nd Map: " + newmap2);   
   }
}
class Student {
   int rollNo;
   String name;

   Student(int rollNo, String name){
      this.rollNo = rollNo;
      this.name = name;
   }

   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + " ]";
   }
}

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

1st Map: {1=[ 1, Julie ], 2=[ 2, Robert ], 3=[ 3, Adam ]}
Cloned 2nd Map: {1=[ 1, Julie ], 2=[ 2, Robert ], 3=[ 3, Adam ]}
java_util_hashmap.htm
Advertisements