Java IdentityHashMap

Introduction

The IdentityHashMap class in Java is a part of the java.util package.

It is a specialized Map implementation that uses reference equality (==) instead of object equality (equals) to compare keys.

Table of Contents

  1. What is the IdentityHashMap Class?
  2. Common Methods
  3. Examples of Using the IdentityHashMap Class
  4. Conclusion

1. What is the IdentityHashMap Class?

The IdentityHashMap class provides a map implementation that compares keys using their reference equality (==) rather than their object equality (equals). This makes it useful for scenarios where the identity of keys is more important than their content.

2. Common Methods

  • put(K key, V value): Associates the specified value with the specified key in this map.
  • get(Object key): Returns the value to which the specified key is mapped.
  • remove(Object key): Removes the mapping for a key from this map if it is present.
  • containsKey(Object key): Returns true if this map contains a mapping for the specified key.
  • containsValue(Object value): Returns true if this map maps one or more keys to the specified value.
  • size(): Returns the number of key-value mappings in this map.
  • isEmpty(): Returns true if this map contains no key-value mappings.
  • clear(): Removes all of the mappings from this map.
  • keySet(): Returns a Set view of the keys contained in this map.
  • values(): Returns a Collection view of the values contained in this map.
  • entrySet(): Returns a Set view of the mappings contained in this map.

3. Examples of Using the IdentityHashMap Class

Example 1: Creating and Using an IdentityHashMap

This example demonstrates how to create and use an IdentityHashMap with key-value pairs.

import java.util.IdentityHashMap;

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

        // Adding elements
        String key1 = new String("key");
        String key2 = new String("key");
        map.put(key1, 1);
        map.put(key2, 2);

        // Retrieving elements
        System.out.println("Value for key1: " + map.get(key1));
        System.out.println("Value for key2: " + map.get(key2));

        // Checking the size of the map
        System.out.println("Size of map: " + map.size());
    }
}

Output:

Value for key1: 1
Value for key2: 2
Size of map: 2

Example 2: Comparing Reference Equality

This example shows how IdentityHashMap uses reference equality instead of object equality.

import java.util.IdentityHashMap;

public class ReferenceEqualityExample {
    public static void main(String[] args) {
        IdentityHashMap<String, String> map = new IdentityHashMap<>();

        // Adding elements
        String key1 = new String("apple");
        String key2 = new String("apple");
        map.put(key1, "fruit");
        map.put(key2, "fruit");

        // Comparing reference equality
        System.out.println("Map contains key1: " + map.containsKey(key1));
        System.out.println("Map contains key2: " + map.containsKey(key2));
        System.out.println("Size of map: " + map.size());
    }
}

Output:

Map contains key1: true
Map contains key2: true
Size of map: 2

Example 3: Removing Elements

This example demonstrates how to remove elements from an IdentityHashMap.

import java.util.IdentityHashMap;

public class RemoveExample {
    public static void main(String[] args) {
        IdentityHashMap<String, Integer> map = new IdentityHashMap<>();
        String key1 = new String("key1");
        String key2 = new String("key2");
        map.put(key1, 1);
        map.put(key2, 2);

        // Removing an element
        map.remove(key1);
        System.out.println("After removing key1: " + map);

        // Checking if map contains a key
        System.out.println("Map contains key2: " + map.containsKey(key2));
    }
}

Output:

After removing key1: {key2=2}
Map contains key2: true

Example 4: Iterating Over Keys and Values

This example shows how to iterate over the keys and values in an IdentityHashMap.

import java.util.IdentityHashMap;
import java.util.Set;
import java.util.Map.Entry;

public class IterateExample {
    public static void main(String[] args) {
        IdentityHashMap<String, Integer> map = new IdentityHashMap<>();
        map.put("key1", 1);
        map.put("key2", 2);

        // Iterating over keys
        Set<String> keys = map.keySet();
        System.out.println("Keys:");
        for (String key : keys) {
            System.out.println(key);
        }

        // Iterating over entries
        Set<Entry<String, Integer>> entries = map.entrySet();
        System.out.println("Entries:");
        for (Entry<String, Integer> entry : entries) {
            System.out.println(entry.getKey() + ": " + entry.getValue());
        }
    }
}

Output:

Keys:
key2
key1
Entries:
key2: 2
key1: 1

4. Conclusion

The IdentityHashMap class in Java provides a specialized map implementation that uses reference equality for comparing keys. It is useful in scenarios where the identity of keys matters more than their content. The examples provided demonstrate common usage patterns and highlight the capabilities of the IdentityHashMap class.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top