Java IdentityHashMap Example

In this tutorial, we will explore the IdentityHashMap class in Java, which is a specialized implementation of the Map interface. Unlike the standard HashMap, IdentityHashMap uses reference equality (==) instead of object equality (equals()) for comparing keys. This tutorial will demonstrate how to use IdentityHashMap with examples, using the latest Java version to ensure modern practices and features.

Table of Contents

  1. Introduction
  2. Prerequisites
  3. Step-by-Step Guide
    1. Creating an IdentityHashMap
    2. Adding and Retrieving Elements
    3. Iterating Over the Map
    4. Comparing IdentityHashMap with HashMap
  4. Complete Code Example
  5. Conclusion

Introduction

IdentityHashMap is a special type of Map that uses reference equality to compare keys. This means that two keys are considered equal if and only if they refer to the same object in memory. This behavior is different from the HashMap, which uses the equals() method to compare keys. IdentityHashMap can be useful in scenarios where reference equality is required.

Prerequisites

Before we start, ensure you have the following:

  • Java Development Kit (JDK) installed (latest version preferred)
  • An Integrated Development Environment (IDE) such as IntelliJ IDEA or Eclipse

Step-by-Step Guide

Step 1: Creating an IdentityHashMap

First, let's create an IdentityHashMap and add some key-value pairs to it.

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

public class IdentityHashMapExample {
    public static void main(String[] args) {
        // Create an IdentityHashMap
        Map<String, Integer> identityMap = new IdentityHashMap<>();

        // Add key-value pairs to the map
        identityMap.put(new String("Ravi"), 25);
        identityMap.put(new String("Sita"), 30);
        identityMap.put(new String("Arjun"), 22);
        identityMap.put(new String("Lakshmi"), 20);

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

Step 2: Adding and Retrieving Elements

Let's add some elements to the IdentityHashMap and retrieve values using different keys.

public class IdentityHashMapExample {
    public static void main(String[] args) {
        // Create an IdentityHashMap
        Map<String, Integer> identityMap = new IdentityHashMap<>();

        // Add key-value pairs to the map
        identityMap.put(new String("Ravi"), 25);
        identityMap.put(new String("Sita"), 30);
        identityMap.put(new String("Arjun"), 22);
        identityMap.put(new String("Lakshmi"), 20);

        // Retrieve and print values using different keys
        System.out.println("Ravi: " + identityMap.get(new String("Ravi")));
        System.out.println("Sita: " + identityMap.get(new String("Sita")));
    }
}

Output:

Ravi: 25
Sita: 30

Step 3: Iterating Over the Map

We can iterate over the IdentityHashMap using the entry set, key set, or values.

public class IdentityHashMapExample {
    public static void main(String[] args) {
        // Create an IdentityHashMap
        Map<String, Integer> identityMap = new IdentityHashMap<>();

        // Add key-value pairs to the map
        identityMap.put(new String("Ravi"), 25);
        identityMap.put(new String("Sita"), 30);
        identityMap.put(new String("Arjun"), 22);
        identityMap.put(new String("Lakshmi"), 20);

        // Iterate over the map using entry set
        for (Map.Entry<String, Integer> entry : identityMap.entrySet()) {
            System.out.println(entry.getKey() + ": " + entry.getValue());
        }
    }
}

Output:

Ravi: 25
Sita: 30
Arjun: 22
Lakshmi: 20

Step 4: Comparing IdentityHashMap with HashMap

Let's compare the behavior of IdentityHashMap with HashMap by adding identical string keys.

import java.util.HashMap;

public class IdentityHashMapExample {
    public static void main(String[] args) {
        // Create a HashMap
        Map<String, Integer> hashMap = new HashMap<>();

        // Add key-value pairs to the map
        hashMap.put(new String("Ravi"), 25);
        hashMap.put(new String("Ravi"), 30);

        // Print the HashMap
        System.out.println("HashMap: " + hashMap);

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

        // Add key-value pairs to the map
        identityMap.put(new String("Ravi"), 25);
        identityMap.put(new String("Ravi"), 30);

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

Output:

HashMap: {Ravi=30}
IdentityHashMap: {Ravi=25, Ravi=30}

In the HashMap, the second put operation overwrites the first value because it uses equals() for key comparison. In the IdentityHashMap, both keys are treated as different because it uses == for key comparison.

Complete Code Example

Here's the complete code example demonstrating various operations with IdentityHashMap:

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

public class IdentityHashMapExample {
    public static void main(String[] args) {
        // Create an IdentityHashMap
        Map<String, Integer> identityMap = new IdentityHashMap<>();

        // Add key-value pairs to the map
        identityMap.put(new String("Ravi"), 25);
        identityMap.put(new String("Sita"), 30);
        identityMap.put(new String("Arjun"), 22);
        identityMap.put(new String("Lakshmi"), 20);

        // Retrieve and print values using different keys
        System.out.println("Ravi: " + identityMap.get(new String("Ravi")));
        System.out.println("Sita: " + identityMap.get(new String("Sita")));

        // Iterate over the map using entry set
        System.out.println("Iterating over IdentityHashMap:");
        for (Map.Entry<String, Integer> entry : identityMap.entrySet()) {
            System.out.println(entry.getKey() + ": " + entry.getValue());
        }

        // Compare IdentityHashMap with HashMap
        // Create a HashMap
        Map<String, Integer> hashMap = new HashMap<>();

        // Add key-value pairs to the map
        hashMap.put(new String("Ravi"), 25);
        hashMap.put(new String("Ravi"), 30);

        // Print the HashMap
        System.out.println("HashMap: " + hashMap);

        // Add key-value pairs to the IdentityHashMap
        identityMap.put(new String("Ravi"), 25);
        identityMap.put(new String("Ravi"), 30);

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

Output:

Ravi: 25
Sita: 30
Iterating over IdentityHashMap:
Ravi: 25
Sita: 30
Arjun: 22
Lakshmi: 20
HashMap: {Ravi=30}
IdentityHashMap: {Ravi=25, Ravi=30, Ravi=25, Ravi=30}

Conclusion

In this tutorial, we demonstrated how to use the IdentityHashMap class in Java. We covered creating an IdentityHashMap, adding and retrieving elements, iterating over the map, and comparing IdentityHashMap with HashMap. By following this guide, developers can effectively use IdentityHashMap in scenarios where reference equality is required.

Comments