IdentityHashMap is a specialized implementation of the Map interface from java.util package. Unlike standard implementations like HashMap, which uses the equals() method for comparisons, IdentityHashMap uses reference equality (==) in place of object equality (equals()) when comparing keys (and values).

Key Points:

Reference Equality: Uses the identity (==) to compare keys, not their equals() method.

Performance: Typically faster than a HashMap for keys that are always distinct objects.

Not Thread-Safe: IdentityHashMap is not synchronized, so if multiple threads access it concurrently, it must be synchronized externally.

Nulls: Both null keys and null values are allowed.

Java IdentityHashMap Common Methods

import java.util.IdentityHashMap;

public class IdentityHashMapMethods {

    public static void main(String[] args) {
        // 1. Create an `IdentityHashMap`
        IdentityHashMap<String, String> identityMap = new IdentityHashMap<>();

        // 2. Add entries to the `IdentityHashMap`
        String key1 = new String("One");
        String key2 = new String("One");
        identityMap.put(key1, "First");
        identityMap.put(key2, "First Duplicate");
        System.out.println("2. IdentityHashMap content: " + identityMap);

        // 3. Check if a key is present
        boolean hasKeyOne = identityMap.containsKey(key1);
        System.out.println("3. Contains key &apos;One&apos;? " + hasKeyOne);

        // 4. Get a value using its key
        String valueOfOne = identityMap.get(key1);
        System.out.println("4. Value for key &apos;One&apos;: " + valueOfOne);

        // 5. Remove an entry using its key
        identityMap.remove(key1);
        System.out.println("5. IdentityHashMap after removing key &apos;One&apos;: " + identityMap);

        // 6. Check the size of the IdentityHashMap
        int size = identityMap.size();
        System.out.println("6. Size of the IdentityHashMap: " + size);
    }
}

Output:

2. IdentityHashMap content: {One=First, One=First Duplicate}
3. Contains key 'One'? true
4. Value for key 'One': First
5. IdentityHashMap after removing key 'One': {One=First Duplicate}
6. Size of the IdentityHashMap: 1

Explanation:

1. Initialized an IdentityHashMap named identityMap.

2. Added key-value pairs using the put() method. Note that IdentityHashMap uses reference-equality in place of object-equality when comparing keys (and values). Thus, two separate String objects with the same content are considered distinct keys.

3. Checked the presence of a specific key with the containsKey() method.

4. Retrieved the value associated with a particular key using the get() method.

5. Removed a key-value pair using the remove() method.

6. Checked the number of key-value pairs using the size() method.

The primary characteristic of an IdentityHashMap is that it uses reference equality (==) when comparing keys and values, unlike the standard HashMap that uses object equality (equals() method).

Using Java IdentityHashMap to Track Object Metadata

import java.util.IdentityHashMap;

class CustomObject {
    private String data;

    public CustomObject(String data) {
        this.data = data;
    }

    public String getData() {
        return data;
    }
}

public class ObjectMetadataTracker {

    public static void main(String[] args) {
        // 1. Create a custom object and its duplicate using new keyword
        CustomObject object1 = new CustomObject("Hello");
        CustomObject object2 = new CustomObject("Hello");

        // 2. Create an `IdentityHashMap` to store metadata about objects
        IdentityHashMap<CustomObject, String> metadataMap = new IdentityHashMap<>();

        // 3. Add metadata to the `IdentityHashMap`
        metadataMap.put(object1, "Object 1 metadata");
        metadataMap.put(object2, "Object 2 metadata");

        // 4. Retrieve metadata for a specific object
        String object1Metadata = metadataMap.get(object1);
        System.out.println("Metadata for object1: " + object1Metadata);

        String object2Metadata = metadataMap.get(object2);
        System.out.println("Metadata for object2: " + object2Metadata);
    }
}

Output:

Metadata for object1: Object 1 metadata
Metadata for object2: Object 2 metadata

Explanation:

1. Created two CustomObject instances, object1 and object2, both holding the same string data "Hello". Though they have the same content, they are distinct objects in memory.

2. Initialized an IdentityHashMap named metadataMap to store metadata about different CustomObject instances.

3. Populated metadataMap with metadata for each CustomObject instance. The IdentityHashMap allows storing metadata for both object1 and object2 distinctly, even though their content is the same.

4. Demonstrated how to retrieve specific metadata for each object using the get() method.

The primary use case here is to store metadata or auxiliary information about distinct objects, even if their content might be the same. IdentityHashMap serves well for this purpose as it checks objects based on their references and not the content.