Java LinkedHashMap putAll() Method

The LinkedHashMap.putAll() method in Java is used to copy all of the mappings from the specified map to the LinkedHashMap.

Table of Contents

  1. Introduction
  2. putAll Method Syntax
  3. Examples
    • Copying Entries from Another Map
  4. Real-World Use Case
    • Example: Merging User Data from Multiple Sources
  5. Conclusion

Introduction

The LinkedHashMap.putAll() method is a member of the LinkedHashMap class in Java. It allows you to copy all of the mappings from the specified map to the current LinkedHashMap. This method is useful when you need to merge multiple maps or initialize a map with predefined mappings.

putAll() Method Syntax

The syntax for the putAll method is as follows:

public void putAll(Map<? extends K, ? extends V> m)
  • The method takes one parameter:
    • m of type Map<? extends K, ? extends V>, which represents the map containing mappings to be copied to the LinkedHashMap.
  • The method does not return a value.

Examples

Copying Entries from Another Map

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

Example

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

public class PutAllExample {
    public static void main(String[] args) {
        // Creating a HashMap with String keys and Integer values
        HashMap<String, Integer> sourceMap = new HashMap<>();

        // Adding entries to the HashMap
        sourceMap.put("Ravi", 25);
        sourceMap.put("Priya", 30);
        sourceMap.put("Vijay", 35);

        // Creating a LinkedHashMap to copy the entries into
        LinkedHashMap<String, Integer> destinationMap = new LinkedHashMap<>();

        // Copying entries from the sourceMap to the destinationMap
        destinationMap.putAll(sourceMap);

        // Printing the LinkedHashMap
        System.out.println("LinkedHashMap after putAll: " + destinationMap);
    }
}

Output:

LinkedHashMap after putAll: {Ravi=25, Priya=30, Vijay=35}

Real-World Use Case

Example: Merging User Data from Multiple Sources

A common real-world use case for LinkedHashMap.putAll() is merging user data from multiple sources. For example, let's consider a scenario where we have two different maps containing user data, and we want to merge them into a single LinkedHashMap.

Example

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

public class MergeUserData {
    public static void main(String[] args) {
        // Creating the first source map with user data
        HashMap<String, Integer> userDataSource1 = new HashMap<>();
        userDataSource1.put("Ramesh", 28);
        userDataSource1.put("Sita", 32);

        // Creating the second source map with additional user data
        HashMap<String, Integer> userDataSource2 = new HashMap<>();
        userDataSource2.put("Hari", 45);
        userDataSource2.put("Priya", 30);

        // Creating a LinkedHashMap to merge the data into
        LinkedHashMap<String, Integer> mergedUserData = new LinkedHashMap<>();

        // Merging data from the first source map
        mergedUserData.putAll(userDataSource1);

        // Merging data from the second source map
        mergedUserData.putAll(userDataSource2);

        // Printing the merged user data
        System.out.println("Merged User Data: " + mergedUserData);
    }
}

Output:

Merged User Data: {Ramesh=28, Sita=32, Hari=45, Priya=30}

In this example, LinkedHashMap.putAll() is used to merge user data from two different maps into a single LinkedHashMap, demonstrating how to handle merging data from multiple sources.

Conclusion

The LinkedHashMap.putAll() method in Java provides a way to copy all mappings from a specified map to a LinkedHashMap. By understanding how to use this method, you can efficiently merge or initialize collections of key-value pairs in your Java applications. The method allows you to handle both the insertion of new mappings and the merging of existing maps, making it a versatile tool for data management.

Comments