Clone a HashMap in Java

In this tutorial, we will see how to Clone a HashMap in Java. 

HashMap can be cloned using clone() method. Cloning of a HashMap returns shallow copy of the HashMap instance, the keys and values are not cloned.

Syntax of clone() method:

the clone() method returns a shallow copy of the HashMap instance, the keys and values are not cloned.

Overrides:
clone in class AbstractMap

Returns:
a shallow copy of this map

Let’s see this with an example.

Example:

Let’s run this program and check the output.

Output:

Related posts

Leave a Comment