JavaGian java tutorial and java interview question and answer

JavaGian , Free Online Tutorials, JavaGian provides tutorials and interview questions of all technology like java tutorial, android, java frameworks, javascript, ajax, core java, sql, python, php, c language etc. for beginners and professionals.

How to Clone a Map in Java collection

 How to Clone a Map in Java collection



Following are the 5 different ways to Clone a Map in Java.


Example:


{1=this, 2=For, 3=this}

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:







// Program to clone a Map in Java 

// Naive Method 

  

import java.util.*; 

  

class JavacloneExample { 

    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, "this"); 

        hash_Map.put(2, "For"); 

        hash_Map.put(3, "this"); 

  

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

    } 

Output:

{1=this, 2=For, 3=this}

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

play_arrow


brightness_4

// Program to clone a Map in Java 

// putAll Method 

  

import java.util.*; 

  

class JavacloneExample { 

  

    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, "this"); 

        hash_Map.put(2, "For"); 

        hash_Map.put(3, "this"); 

  

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

    } 

Output:

{1=this, 2=For, 3=this}

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:






  

import java.util.*; 

  

class JavacloneExample { 

  

    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, "this"); 

        hash_Map.put(2, "For"); 

        hash_Map.put(3, "this"); 

  

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

    } 

Output:

{1=this, 2=For, 3=this}

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:



  

import java.util.*; 

import java.util.stream.Collectors; 

  

class JavacloneExample { 

  

    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, "this"); 

        hash_Map.put(2, "For"); 

        hash_Map.put(3, "this"); 

  

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

    } 

Output:

{1=this, 2=For, 3=this}

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



  

import java.util.*; 

import java.util.stream.Collectors; 

import com.google.gson.Gson; 

  

class JavacloneExample { 

  

    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, "this"); 

        hash_Map.put(2, "For"); 

        hash_Map.put(3, "this"); 

  

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

    } 

Output:

{1=this, 2=For, 3=this}


.