Given a map, the task is to clone that map.
Following are the 5 different ways to Clone a Map in Java.
Example:
{1=Geeks, 2=For, 3=Geeks}
1. Create an object for the class map.
2. Put the elements into the map using the put() method.
3. Again create another object for the class map.
4. Now finally iterate the map and call put method to clone the initial map.
Below is the implementation of the above approach:
Implementation:
// Program to clone a Map in Java// Naive Method  import java.util.*;  class GFG {    public static void main(String[] args)    {        // Creating an object for class Map        Map<Integer, String> hash_Map            = new HashMap<Integer, String>();          // putting elements into the map        hash_Map.put(1, "Geeks");        hash_Map.put(2, "For");        hash_Map.put(3, "Geeks");          // Creating a new object for        // class Map to clone a map        Map<Integer, String> new_map            = new HashMap<Integer, String>();          // using iterator        for (Map.Entry<Integer, String> entry : hash_Map.entrySet()) {            new_map.put(entry.getKey(),                        entry.getValue());        }          System.out.println(new_map);    }} |
{1=Geeks, 2=For, 3=Geeks}
1. Create an object for the class map.
2. Put the elements into the map using the put() method.
3. Again create another object for the class map.
4. Now finally use putAll() method to clone the initial map.
Below is the implementation of the above approach:
Implementation:
// Program to clone a Map in Java// putAll Method  import java.util.*;  class GFG {      public static void main(String[] args)    {        // Creating an object for class Map        Map<Integer, String> hash_Map            = new HashMap<Integer, String>();          // Putting elements into the map        hash_Map.put(1, "Geeks");        hash_Map.put(2, "For");        hash_Map.put(3, "Geeks");          // Creating a new object        // for class Map to clone a map        Map<Integer, String> new_map            = new HashMap<Integer, String>();          // using putAll method        new_map.putAll(hash_Map);          System.out.println(new_map);    }} |
{1=Geeks, 2=For, 3=Geeks}
1. Create an object for the class map.
2. Put the elements into the map using the put() method.
3. Again create another object for the class map.
4. Now finally use the copy constructor(It is a special constructor used for creating a new object as a copy of an existing object) to clone the initial map.
Below is the implementation of the above approach:
Implementation:
// Program to clone a Map in Java// Copy Constructor  import java.util.*;  class GFG {      public static void main(String[] args)    {        // Creating an object for class Map        Map<Integer, String> hash_Map            = new HashMap<Integer, String>();          // putting elements into the map        hash_Map.put(1, "Geeks");        hash_Map.put(2, "For");        hash_Map.put(3, "Geeks");          // Creating a new object        // for class Map to clone a map        Map<Integer, String> new_map            = new HashMap<Integer, String>();          new_map = new HashMap<>(hash_Map);        System.out.println(new_map);    }} |
{1=Geeks, 2=For, 3=Geeks}
1. Create an object for the class map.
2. Put the elements into the map using the put() method.
3. Again create another object for the class map.
4. Now finally use Stream API from Java 8 to clone the initial map.
Below is the implementation of the above approach:
Implementation:
// Program to clone a Map in Java// Using java8  import java.util.*;import java.util.stream.Collectors;  class GFG {      public static void main(String[] args)    {        // Creating an object for class Map        Map<Integer, String> hash_Map            = new HashMap<Integer, String>();          // putting elements into the map        hash_Map.put(1, "Geeks");        hash_Map.put(2, "For");        hash_Map.put(3, "Geeks");          // Creating a new object        // for class Map to clone a map        Map<Integer, String> new_map            = new HashMap<Integer, String>();          new_map            = hash_Map.entrySet()                  .stream()                  .collect(                      Collectors                          .toMap(Map.Entry::getKey,                                 Map.Entry::getValue));          System.out.println(new_map);    }} |
{1=Geeks, 2=For, 3=Geeks}
Explanation:It is also similar to above methods, but here we use Stream API from java 8 to clone the original map.
1. Create an object for the class map.
2. Put the elements into the map using the put() method.
3. Again create another object for the class map.
4. Now finally use Google’s GSON library to clone the initial map.
Below is the implementation of the above approach:
Implementation:
// Program to clone a Map in Java// JSON Method  import java.util.*;import java.util.stream.Collectors;import com.google.gson.Gson;  class GFG {      public static void main(String[] args)    {        // Creating an object for class Map        Map<Integer, String> hash_Map            = new HashMap<Integer, String>();          // putting elements into the map        hash_Map.put(1, "Geeks");        hash_Map.put(2, "For");        hash_Map.put(3, "Geeks");          // Creating a new object        // for class Map to clone a map        Map<Integer, String> new_map            = new HashMap<Integer, String>();        Gson gson = new Gson();          String jsonString = gson.toJson(hash_Map);          new_map = gson.fromJson(jsonString, Map.class);          System.out.println(new_map);    }} |
{1=Geeks, 2=For, 3=Geeks}
Explanation:It is also similar to the above methods, but here we use Google’s GSON library to clone the original map. Here initially we convert Map to a JSON string and later we convert that string to a new map.
