Java IdentityHashMap keySet() Method

The IdentityHashMap.keySet() method in Java returns a Set view of the keys contained in the map. 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.keySet() can be used effectively.

Table of Contents

  1. Introduction
  2. keySet Method Syntax
  3. Examples
    • Basic Usage of keySet Method
    • Iterating Over Keys
  4. Real-World Use Case
    • Example: Managing User Sessions
  5. Conclusion

Introduction

The IdentityHashMap.keySet() method is a member of the IdentityHashMap class in Java. This class uses reference equality (==) instead of object equality (equals()) when comparing keys. The keySet method returns a Set view of the keys contained in the map.

keySet() Method Syntax

The syntax for the keySet method is as follows:

public Set<K> keySet()
  • Parameters: This method does not take any parameters.
  • Returns: A set view of the keys contained in this map.

Examples

Basic Usage of keySet Method

The keySet method can be used to get a set view of the keys contained in the IdentityHashMap.

Example

import java.util.IdentityHashMap;
import java.util.Set;

public class IdentityHashMapKeySetExample {
    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);

        // Getting the set view of the keys
        Set<String> keySet = map.keySet();

        // Printing the set view of the keys
        System.out.println("IdentityHashMap Key Set: " + keySet);
    }
}

Output:

IdentityHashMap Key Set: [Ravi, Priya, Vijay]

Iterating Over Keys

You can iterate over the keys in the IdentityHashMap using the set view returned by the keySet method.

Example

import java.util.IdentityHashMap;
import java.util.Set;

public class IdentityHashMapIterateKeysExample {
    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);

        // Getting the set view of the keys
        Set<String> keySet = map.keySet();

        // Iterating over the keys
        for (String key : keySet) {
            System.out.println("Key: " + key);
        }
    }
}

Output:

Key: Ravi
Key: Priya
Key: Vijay

Real-World Use Case

Example: Managing User Sessions

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

Example

import java.util.IdentityHashMap;
import java.util.Set;

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 user sessions
        UserSession session1 = new UserSession("S1", "Ravi");
        UserSession session2 = new UserSession("S2", "Priya");
        UserSession session3 = new UserSession("S3", "Vijay");

        // Creating an IdentityHashMap to manage user sessions
        IdentityHashMap<UserSession, String> sessionMap = new IdentityHashMap<>();
        sessionMap.put(session1, "Active");
        sessionMap.put(session2, "Inactive");
        sessionMap.put(session3, "Active");

        // Getting the set view of the keys (user sessions)
        Set<UserSession> keySet = sessionMap.keySet();

        // Displaying the user sessions
        for (UserSession session : keySet) {
            System.out.println("User Session: " + session);
        }
    }
}

Output:

User Session: UserSession{sessionId='S1', userName='Ravi'}
User Session: UserSession{sessionId='S2', userName='Priya'}
User Session: UserSession{sessionId='S3', userName='Vijay'}

In this example, IdentityHashMap.keySet() 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.keySet() method in Java provides a way to get a set view of the keys contained 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