Java HashMap putAll() Method

The HashMap.putAll() method in Java is used to copy all of the mappings from the specified map to this map. This guide will cover the method's usage, explain how it works, and provide examples to demonstrate its functionality.

Table of Contents

  1. Introduction
  2. putAll Method Syntax
  3. Examples
    • Copying All Mappings from Another Map
    • Real-World Use Case: Merging Employee Databases
  4. Conclusion

Introduction

The putAll() method is a member of the HashMap class in Java. It allows you to copy all of the mappings from the specified map to this map. This can be useful when you need to combine the contents of two maps into one.

putAll() Method Syntax

The syntax for the putAll method is as follows:

public void putAll(Map<? extends K, ? extends V> m)
  • The method takes a single parameter m of type Map<? extends K, ? extends V>, which represents the map whose mappings are to be copied to this map.
  • The method does not return any value.

Examples

Copying All Mappings from Another Map

The putAll method can be used to copy all mappings from one map to another.

Example

import java.util.HashMap;

public class PutAllExample {
    public static void main(String[] args) {
        // Creating a HashMap with String keys and Integer values
        HashMap<String, Integer> map1 = new HashMap<>();
        map1.put("Ravi", 25);
        map1.put("Priya", 30);

        // Creating another HashMap with String keys and Integer values
        HashMap<String, Integer> map2 = new HashMap<>();
        map2.put("Vijay", 35);
        map2.put("Anita", 28);

        // Copying all mappings from map2 to map1
        map1.putAll(map2);

        // Printing the resulting HashMap
        System.out.println("Combined HashMap: " + map1);
    }
}

Output:

Combined HashMap: {Ravi=25, Priya=30, Vijay=35, Anita=28}

Real-World Use Case: Merging Employee Databases

In a real-world scenario, you might use the putAll method to merge employee details from two different departments into a single database.

Example

import java.util.HashMap;

public class MergeEmployeeDatabases {
    public static void main(String[] args) {
        // Creating a HashMap for the first department
        HashMap<String, String> dept1 = new HashMap<>();
        dept1.put("E001", "Ravi Kumar");
        dept1.put("E002", "Priya Sharma");

        // Creating a HashMap for the second department
        HashMap<String, String> dept2 = new HashMap<>();
        dept2.put("E003", "Vijay Singh");
        dept2.put("E004", "Anita Rao");

        // Merging the second department's employee details into the first department's map
        dept1.putAll(dept2);

        // Printing the merged employee database
        System.out.println("Merged Employee Database: " + dept1);
    }
}

Output:

Merged Employee Database: {E001=Ravi Kumar, E002=Priya Sharma, E003=Vijay Singh, E004=Anita Rao}

Conclusion

The HashMap.putAll() method in Java provides a way to copy all of the mappings from one map to another. By understanding how to use this method, you can efficiently combine the contents of two maps. This method is useful in various scenarios, such as merging data from different sources or consolidating information into a single data structure.

Comments