Java IdentityHashMap put() Method

The IdentityHashMap.put() method in Java is used to associate the specified value with the specified key in the map. If the map previously contained a mapping for the key, the old value is replaced. 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.put() can be used effectively.

Table of Contents

  1. Introduction
  2. put Method Syntax
  3. Examples
    • Basic Usage of put Method
    • Updating Existing Mappings
  4. Real-World Use Case
    • Example: Managing User Sessions with IdentityHashMap
  5. Conclusion

Introduction

The IdentityHashMap.put() method is a member of the IdentityHashMap class in Java. This class uses reference equality (==) instead of object equality (equals()) when comparing keys. The put method associates the specified value with the specified key in the map. If the map previously contained a mapping for the key, the old value is replaced.

put() Method Syntax

The syntax for the put method is as follows:

public V put(K key, V value)
  • Parameters:
    • key: The key with which the specified value is to be associated.
    • value: The value to be associated with the specified key.
  • Returns: The previous value associated with key, or null if there was no mapping for key.

Examples

Basic Usage of put Method

The put method can be used to add key-value pairs to an IdentityHashMap.

Example

import java.util.IdentityHashMap;

public class IdentityHashMapPutExample {
    public static void main(String[] args) {
        // Creating an IdentityHashMap
        IdentityHashMap<String, Integer> map = new IdentityHashMap<>();

        // Adding key-value pairs to the IdentityHashMap
        map.put("Ravi", 25);
        map.put("Priya", 30);
        map.put("Vijay", 35);

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

Output:

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

Updating Existing Mappings

If the map previously contained a mapping for the key, the old value is replaced with the new value.

Example

import java.util.IdentityHashMap;

public class IdentityHashMapUpdateExample {
    public static void main(String[] args) {
        // Creating an IdentityHashMap
        IdentityHashMap<String, Integer> map = new IdentityHashMap<>();

        // Adding key-value pairs to the IdentityHashMap
        map.put("Ravi", 25);
        map.put("Priya", 30);

        // Updating an existing key-value pair
        Integer oldValue = map.put("Ravi", 28);

        // Printing the old value and the updated IdentityHashMap
        System.out.println("Old value for 'Ravi': " + oldValue);
        System.out.println("Updated IdentityHashMap: " + map);
    }
}

Output:

Old value for 'Ravi': 25
Updated IdentityHashMap: {Ravi=28, Priya=30}

Real-World Use Case

Example: Managing User Sessions with IdentityHashMap

A common real-world use case for IdentityHashMap.put() is managing user sessions in a web application where reference equality is required.

Example

import java.util.IdentityHashMap;

public class UserSessionManager {
    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 an IdentityHashMap to manage user sessions
        IdentityHashMap<UserSession, String> sessionMap = new IdentityHashMap<>();

        // Creating user sessions
        UserSession session1 = new UserSession("S1", "Ravi");
        UserSession session2 = new UserSession("S2", "Priya");

        // Adding user sessions to the IdentityHashMap
        sessionMap.put(session1, "Active");
        sessionMap.put(session2, "Active");

        // Printing the IdentityHashMap
        System.out.println("Session Map: " + sessionMap);

        // Updating a user session status
        sessionMap.put(session1, "Inactive");

        // Printing the updated IdentityHashMap
        System.out.println("Updated Session Map: " + sessionMap);
    }
}

Output:

Session Map: {UserSession{sessionId='S1', userName='Ravi'}=Active, UserSession{sessionId='S2', userName='Priya'}=Active}
Updated Session Map: {UserSession{sessionId='S1', userName='Ravi'}=Inactive, UserSession{sessionId='S2', userName='Priya'}=Active}

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

Conclusion

The IdentityHashMap.put() method in Java provides a way to associate a specified value with a specified key in the map, using reference equality for key comparison. By understanding how to use this method, you can efficiently manage 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