Java HashMap putAll() Method

The HashMap.putAll() method in Java is used to copy all the key-value pair mappings from the specified map to the HashMap.

Table of Contents

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

Introduction

The HashMap.putAll() method is a member of the HashMap class in Java. It allows you to copy all mappings from another map to the current HashMap. This can be useful when you need to merge multiple maps or copy data from one map to another.

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 the HashMap.
  • The method does not return any value.

Examples

Copying All Entries from Another Map

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

Example

import java.util.HashMap;
import java.util.Map;

public class PutAllExample {
    public static void main(String[] args) {
        // Creating a Map with String keys and String values
        Map<String, String> countries1 = new HashMap<>();
        countries1.put("India", "IN");
        countries1.put("United States", "US");

        // Creating another Map with String keys and String values
        Map<String, String> countries2 = new HashMap<>();
        countries2.put("Canada", "CA");
        countries2.put("Australia", "AU");

        // Copying all entries from countries2 to countries1
        countries1.putAll(countries2);

        // Printing the updated countries1
        System.out.println("Updated countries1: " + countries1);
    }
}

Output:

Updated countries1: {India=IN, United States=US, Canada=CA, Australia=AU}

Real-World Use Case: Merging Employee Databases

In a real-world scenario, you might use the putAll method to merge employee databases from different departments.

Example

import java.util.HashMap;
import java.util.Map;

public class MergeEmployeeDatabases {
    public static void main(String[] args) {
        // Creating a Map with String keys (employee IDs) and String values (employee names) for department 1
        Map<String, String> department1 = new HashMap<>();
        department1.put("E001", "Ravi Kumar");
        department1.put("E002", "Priya Sharma");

        // Creating another Map with String keys (employee IDs) and String values (employee names) for department 2
        Map<String, String> department2 = new HashMap<>();
        department2.put("E003", "Vijay Singh");
        department2.put("E004", "Amit Patel");

        // Merging employee databases from department2 into department1
        department1.putAll(department2);

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

Output:

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

Conclusion

The HashMap.putAll() method in Java provides a way to copy all mappings from another map to the current HashMap. By understanding how to use this method, you can efficiently merge and copy data between maps in your Java applications. This method is useful in various scenarios, such as merging databases, copying configuration settings, and managing collections of data.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top