How to Clone a Map in Java

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}
  • Method 1: Naive method
    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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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);
        }
    }

    chevron_right

    
    

    Output:

    {1=Geeks, 2=For, 3=Geeks}
    
  • Method 2: Using putAll().
    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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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);
        }
    }

    chevron_right

    
    

    Output:

    {1=Geeks, 2=For, 3=Geeks}
    
  • Method 3: Copy Constructor.
    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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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);
        }
    }

    chevron_right

    
    

    Output:



    {1=Geeks, 2=For, 3=Geeks}
    
  • Method 4: Java 8
    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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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);
        }
    }

    chevron_right

    
    

    Output:

    {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.

  • Method 5: JSON
    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:

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    // 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);
        }
    }

    chevron_right

    
    

    Output:

    {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.



  • My Personal Notes arrow_drop_up

    Check out this Author's contributed articles.

    If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

    Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




    Article Tags :
    Practice Tags :


    Be the First to upvote.


    Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.