How to Clone a Map in Java
Last Updated :
21 Jan, 2019
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:
import java.util.*;
class GFG {
public static void main(String[] args)
{
Map<Integer, String> hash_Map
= new HashMap<Integer, String>();
hash_Map.put( 1 , "Geeks" );
hash_Map.put( 2 , "For" );
hash_Map.put( 3 , "Geeks" );
Map<Integer, String> new_map
= new HashMap<Integer, String>();
for (Map.Entry<Integer, String> entry : hash_Map.entrySet()) {
new_map.put(entry.getKey(),
entry.getValue());
}
System.out.println(new_map);
}
}
|
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:
import java.util.*;
class GFG {
public static void main(String[] args)
{
Map<Integer, String> hash_Map
= new HashMap<Integer, String>();
hash_Map.put( 1 , "Geeks" );
hash_Map.put( 2 , "For" );
hash_Map.put( 3 , "Geeks" );
Map<Integer, String> new_map
= new HashMap<Integer, String>();
new_map.putAll(hash_Map);
System.out.println(new_map);
}
}
|
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:
import java.util.*;
class GFG {
public static void main(String[] args)
{
Map<Integer, String> hash_Map
= new HashMap<Integer, String>();
hash_Map.put( 1 , "Geeks" );
hash_Map.put( 2 , "For" );
hash_Map.put( 3 , "Geeks" );
Map<Integer, String> new_map
= new HashMap<Integer, String>();
new_map = new HashMap<>(hash_Map);
System.out.println(new_map);
}
}
|
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:
import java.util.*;
import java.util.stream.Collectors;
class GFG {
public static void main(String[] args)
{
Map<Integer, String> hash_Map
= new HashMap<Integer, String>();
hash_Map.put( 1 , "Geeks" );
hash_Map.put( 2 , "For" );
hash_Map.put( 3 , "Geeks" );
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=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:
import java.util.*;
import java.util.stream.Collectors;
import com.google.gson.Gson;
class GFG {
public static void main(String[] args)
{
Map<Integer, String> hash_Map
= new HashMap<Integer, String>();
hash_Map.put( 1 , "Geeks" );
hash_Map.put( 2 , "For" );
hash_Map.put( 3 , "Geeks" );
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=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.
Share your thoughts in the comments
Please Login to comment...