Creating a HashMap from another HashMap in Java

In Java, a HashMap is a commonly used data structure that stores key-value pairs. Sometimes, we may need to create a new HashMap from an existing HashMap, either to make a copy or to perform some transformations on the elements. In this blog post, we will explore how to create a new HashMap from another HashMap.

Method 1: Using the HashMap constructor

The simplest way to create a new HashMap from an existing HashMap is by using the HashMap class’s constructor that accepts another Map as a parameter. This constructor creates a new HashMap with the same initial capacity and load factor as the original HashMap, and adds all the key-value pairs from the original HashMap to the new HashMap.

// Existing HashMap
HashMap<String, Integer> originalMap = new HashMap<>();
originalMap.put("A", 1);
originalMap.put("B", 2);
originalMap.put("C", 3);

// Create new HashMap from the original
HashMap<String, Integer> newMap = new HashMap<>(originalMap);

In the above example, we first create an originalMap HashMap and populate it with some key-value pairs. Then, we create a new HashMap called newMap by passing the originalMap to the constructor. The newMap will now have the same key-value pairs as the originalMap.

Method 2: Using the putAll() method

Another way to create a new HashMap from an existing HashMap is by using the putAll() method. This method copies all the key-value pairs from one Map into another Map.

// Existing HashMap
HashMap<String, Integer> originalMap = new HashMap<>();
originalMap.put("A", 1);
originalMap.put("B", 2);
originalMap.put("C", 3);

// Create new HashMap and copy all elements from the original
HashMap<String, Integer> newMap = new HashMap<>();
newMap.putAll(originalMap);

In this approach, we first create an empty HashMap called newMap. Then, we use the putAll() method to copy all the elements from the originalMap to the newMap.

Conclusion

In this blog post, we explored two methods to create a new HashMap from an existing HashMap in Java. Whether you choose to use the constructor or the putAll() method depends on your specific requirements and coding style. It’s important to keep in mind that when creating a new HashMap from another HashMap, the new HashMap is a separate instance and any changes made to one HashMap will not affect the other.

I hope you found this blog post helpful! If you have any questions or feedback, please leave a comment below.

References: