Java IdentityHashMap putAll() Method

The IdentityHashMap.putAll() method in Java is used to copy all mappings from the specified map to the IdentityHashMap. This guide will cover the method's usage, explain how it works, and provide examples to demonstrate its functionality. We will also cover a real-world use case to show how IdentityHashMap.putAll() can be used effectively.

Table of Contents

  1. Introduction
  2. putAll Method Syntax
  3. Examples
    • Basic Usage of putAll Method
    • Merging Maps with IdentityHashMap
  4. Real-World Use Case
    • Example: Consolidating User Sessions from Multiple Sources
  5. Conclusion

Introduction

The IdentityHashMap.putAll() method is a member of the IdentityHashMap class in Java. This class uses reference equality (==) instead of object equality (equals()) when comparing keys. The putAll method copies all of the mappings from the specified map to the IdentityHashMap.

putAll() Method Syntax

The syntax for the putAll method is as follows:

public void putAll(Map<? extends K, ? extends V> m)
  • Parameters:
    • m: The map containing mappings to be added to this map.
  • Returns: This method does not return a value.

Examples

Basic Usage of putAll Method

The putAll method can be used to copy all mappings from the specified map to the IdentityHashMap.

Example

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

public class IdentityHashMapPutAllExample {
    public static void main(String[] args) {
        // Creating a regular HashMap
        Map<String, Integer> hashMap = new HashMap<>();
        hashMap.put("Ravi", 25);
        hashMap.put("Priya", 30);

        // Creating an IdentityHashMap
        IdentityHashMap<String, Integer> identityMap = new IdentityHashMap<>();
        identityMap.put("Vijay", 35);

        // Copying all mappings from hashMap to identityMap
        identityMap.putAll(hashMap);

        // Printing the IdentityHashMap
        System.out.println("IdentityHashMap: " + identityMap);
    }
}

Output:

IdentityHashMap: {Ravi=25, Priya=30, Vijay=35}

Merging Maps with IdentityHashMap

You can use the putAll method to merge mappings from different maps into an IdentityHashMap.

Example

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

public class IdentityHashMapMergeExample {
    public static void main(String[] args) {
        // Creating the first map
        Map<String, Integer> map1 = new HashMap<>();
        map1.put("Ravi", 25);
        map1.put("Priya", 30);

        // Creating the second map
        Map<String, Integer> map2 = new HashMap<>();
        map2.put("Vijay", 35);
        map2.put("Anita", 28);

        // Creating an IdentityHashMap
        IdentityHashMap<String, Integer> identityMap = new IdentityHashMap<>();

        // Merging mappings from both maps into the IdentityHashMap
        identityMap.putAll(map1);
        identityMap.putAll(map2);

        // Printing the IdentityHashMap
        System.out.println("IdentityHashMap after merging: " + identityMap);
    }
}

Output:

IdentityHashMap after merging: {Ravi=25, Priya=30, Vijay=35, Anita=28}

Real-World Use Case

Example: Consolidating User Sessions from Multiple Sources

A common real-world use case for IdentityHashMap.putAll() is consolidating user sessions from multiple sources where reference equality is crucial.

Example

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

public class UserSessionConsolidator {
    static class UserSession {
        private String sessionId;
        private String userName;

        public UserSession(String sessionId, String userName) {
            this.sessionId = sessionId;
            this.userName = userName;
        }

        @Override
        public String toString() {
            return "UserSession{" +
                    "sessionId='" + sessionId + '\'' +
                    ", userName='" + userName + '\'' +
                    '}';
        }
    }

    public static void main(String[] args) {
        // Creating user sessions from source 1
        Map<UserSession, String> source1Sessions = new HashMap<>();
        UserSession session1 = new UserSession("S1", "Ravi");
        UserSession session2 = new UserSession("S2", "Priya");
        source1Sessions.put(session1, "Active");
        source1Sessions.put(session2, "Inactive");

        // Creating user sessions from source 2
        Map<UserSession, String> source2Sessions = new HashMap<>();
        UserSession session3 = new UserSession("S3", "Vijay");
        UserSession session4 = new UserSession("S4", "Anita");
        source2Sessions.put(session3, "Active");
        source2Sessions.put(session4, "Active");

        // Creating an IdentityHashMap to consolidate sessions
        IdentityHashMap<UserSession, String> consolidatedSessions = new IdentityHashMap<>();

        // Adding sessions from both sources to the IdentityHashMap
        consolidatedSessions.putAll(source1Sessions);
        consolidatedSessions.putAll(source2Sessions);

        // Printing the consolidated IdentityHashMap
        System.out.println("Consolidated Sessions: " + consolidatedSessions);
    }
}

Output:

Consolidated Sessions: {UserSession{sessionId='S1', userName='Ravi'}=Active, UserSession{sessionId='S2', userName='Priya'}=Inactive, UserSession{sessionId='S3', userName='Vijay'}=Active, UserSession{sessionId='S4', userName='Anita'}=Active}

In this example, IdentityHashMap.putAll() is used to consolidate user sessions from multiple sources, where sessions are identified by reference equality, making it suitable for scenarios where unique object references are crucial.

Conclusion

The IdentityHashMap.putAll() method in Java provides a way to copy all mappings from the specified map to the IdentityHashMap, using reference equality for key comparison. By understanding how to use this method, you can efficiently manage and merge collections of key-value pairs where reference equality is required. This method allows you to utilize the power of IdentityHashMap for various scenarios, making it a versatile tool for managing collections of key-value pairs based on reference equality.

Comments