Java HashMap.clone() – Examples

In this tutorial, we will learn about the Java HashMap.clone() function, and learn how to use this function to make a shall copy of the HashMap, with the help of examples.

clone()

HashMap.clone() returns a shallow copy of this HashMap instance. The keys and values themselves are not cloned.

If the values are user defined objects or other high level objects, then they are not cloned, but the references are copied. Any modifications done to the objects in mappings of the original HashMap would reflect the changes in cloned Hashap and vice-versa.

Syntax

The syntax of clone() function is

clone()

Returns

The function returns Object.

Example 1 – clone()

In this example, we will initialize a HashMap hashMap with some mappings. Then we will clone this haspMap to hashMap2.

Java Program

import java.util.HashMap;

public class Example{ 
	public static void main(String[] args) {
		HashMap<String,Integer> hashMap = new HashMap<>();
		hashMap.put("A",1);
		hashMap.put("B",2);
		hashMap.put("C",3);
		hashMap.put("D",4);
		System.out.println("Original HashMap : " + hashMap);

		HashMap<String,Integer> hashMap2 = (HashMap) hashMap.clone();
		System.out.println("Cloned   HashMap : " + hashMap2);
	}
}

Output

Original HashMap : {A=1, B=2, C=3, D=4}
Cloned   HashMap : {A=1, B=2, C=3, D=4}

Example 2 – clone() – Modify Clone

In this example, we will initialize a HashMap hashMap with some mappings. Then we will clone this haspMap to hashMap2. After that, we will make change the value for "C" in hashMap, and also change the value for "B" in hashMap2.

Even though clone() does a shallow copy, as integer is a primitive datatype, any modifications done to original HashMap does not reflect to cloned HashMap and vice-versa.

Java Program

import java.util.HashMap;

public class Example{ 
	public static void main(String[] args) {
		HashMap<String,Integer> hashMap = new HashMap<>();
		hashMap.put("A",1);
		hashMap.put("B",2);
		hashMap.put("C",3);
		hashMap.put("D",4);
		
		//clone
		HashMap<String,Integer> hashMap2 = (HashMap) hashMap.clone();
		
		//modify original
		hashMap.put("C",5);
		//modify clone
		hashMap2.put("B",8);
		
		System.out.println("Original HashMap : " + hashMap);
		System.out.println("Cloned   HashMap : " + hashMap2);
	}
}

Output

Original HashMap : {A=1, B=2, C=5, D=4}
Cloned   HashMap : {A=1, B=8, C=3, D=4}

Example 3 – clone() – HashMap = null

In this example, we will take null value for hashMap. If we call clone() on this null HashMap, the method should throw java.lang.NullPointerException.

Java Program

import java.util.HashMap;

public class Example{ 
	public static void main(String[] args) {
		HashMap<String,Integer> hashMap = null;
		
		//clone
		HashMap<String,Integer> hashMap2 = (HashMap) hashMap.clone();
		
		System.out.println("Original HashMap : " + hashMap);
		System.out.println("Cloned   HashMap : " + hashMap2);
	}
}

Output

Exception in thread "main" java.lang.NullPointerException
	at Example.main(Example.java:8)

Conclusion

In this Java Tutorial, we have learnt the syntax of Java HashMap.clone() function, and also learnt how to use this function with the help of examples.