Javatpoint Logo
Javatpoint Logo

Clone HashMap in Java

In Java, HashMap is a Hashtable-based implementation. The implementation of the HashMap allows us to apply all the optional Map operations like add data to Map, delete data from the Map, retrieve key-value from the Map, determine the Map size, etc. Besides these, we can also create a clone or copy of a Map. In this section, we will learn how to clone HashMap in Java.

Java HashMap

Java HashMap class implements the Map interface which allows us to store key and value pairs, where keys should be unique. If we try to insert the duplicate key, it will replace the element of the corresponding key. It is easy to perform operations using the key index like updation, deletion, etc. The Java HashMap class belongs to java.util package.

HashMap in Java is like the legacy Hashtable class, but it is not synchronized. It allows us to store the null elements as well, but there should be only one null key. Since Java 5, it is denoted as HashMap<K, V>, where K stands for key and V for value. It inherits the AbstractMap class and implements the Map interface.

Methods to Clone a HashMap

There are the following ways to clone a Map in Java:

  • Naive Solution
  • Using Map.putAll() Method
  • Using Copy Constructor
  • Using Java 8 Stream
  • Using Google's JSON Library
  • Using HashMap.clone() Method

Naive Solution

It is the simplest way to clone a HashMap. In this method, we iterate the HashMap and invoke the put(k, v) method once for each mapping from key to value. Let's see a program for the same.

CloneHashMapExample1.java

Output:

{Bangladesh=75, Sri Lanka=65, Nepal=76, India=101}

Using Map.putAll() Method

Another way to clone a map is to use the putAll() method. It copies all the mapping from the original map to an empty map.

CloneHashMapExample2.java

Output:

{M.Tech=60, MCA=90, B.Arch=200, B.Tech=120, B.Pharma=50}

Using Copy Constructor

We can also use a copy constructor to clone a map. In this method, a constructor of the Map is used to create a new object as a copy of an existing object.

CloneHashMapExample3.java

Output:

{Five=5, Two=2, Three=3, One=1, Four=4}

Using Java 8 Stream

Java Stream API can also be used to clone a HashMap. Let's see an example.

CloneHashMapExample4.java

Output:

{Mac Book=67000.0, Dell PC=34000.0, Acer Pro=55000.0, Asus Gaming Laptop=89000.0, HP Note Book=45000.0}

Using Google's JSON Library

Another way to clone a HashMap is to use Google's JSON library. In this method, first, convert the map to JSON string and JSON string back to a new map object. It works the same as the serialized object holds no reference to the original object on deserialization.

CloneHashMapExample5.java

Output:

{Java=7, Python=2, C++=9, COBOL=12, C#=4, Kotlin=4}

Using HashMap.clone() Method

Java HashMap class provides the clone() method that returns a shallow copy of the HashMap instance. Note that keys and values are not cloned. The clone() method of the HashMap class overrides the clone() method of the AbstractMap<K, V>.

Syntax:

Example of HashMap.clone() Method

CloneHashMapExample6.java

Output:

Initial Mappings are: {50=Papaya, 22=Grapes, 90=Pineapple, 13=Blue Barriers, 110=Apple}
The cloned map looks like this: {50=Papaya, 90=Pineapple, 13=Blue Barriers, 22=Grapes, 110=Apple}






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA