Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Java

IdentityHashMap: put, get, containsKey, and size

`IdentityHashMap` matches keys with `==`, not `equals`. Learn when `put` replaces an entry, what `size` counts, and how to tell a missing key from one mapped to `null`.

Version: Java SE 21

Syntax

import java.util.IdentityHashMap;

// Instance method signatures on IdentityHashMap<K, V>:
public V put(K key, V value)
public V get(Object key)
public boolean containsKey(Object key)
public int size()

In Java SE 21, java.util.IdentityHashMap<K, V> matches a key to a stored key exactly when key == storedKey. Two references to the same object therefore identify one key, while two distinct objects can be separate keys even if equals returns true. The map does not call the key's equals or hashCode to find a match: it hashes by identity using System.identityHashCode, then uses == to decide whether keys match.

In IdentityHashMap.put/get/containsKey/size, the identity rule determines whether a write adds an entry or updates one. put(K key, V value) inserts an entry if the key is absent and otherwise replaces its value. It returns the previous value, or null if the key was absent or previously mapped to null. size() returns the number of entries as an int: insertion adds one, while replacement leaves the count unchanged.

get(Object key) returns the mapped value, or null if the key is absent. The map allows both a null key and null values, so get alone cannot tell you whether an entry exists. containsKey(Object key) returns true exactly when there is an entry for that key, regardless of its value.

Although this class implements Map, it deliberately departs from the interface's general equals-based equality contract. Use it when the particular instance matters, such as when tracking visited nodes in an object graph. Do not substitute it for HashMap when keys should compare by value.

Examples

import java.util.IdentityHashMap;

public class Main {
    public static void main(String[] args) {
        String a = new String("node");
        String b = new String("node");
        String alias = a;
        IdentityHashMap<String, String> map = new IdentityHashMap<>();

        System.out.println("a.equals(b): " + a.equals(b));
        System.out.println("a == b: " + (a == b));
        System.out.println("put(a): " + map.put(a, "first"));
        System.out.println("get(alias): " + map.get(alias));
        System.out.println("put(alias): " + map.put(alias, "updated"));
        System.out.println("size after replacement: " + map.size());

        System.out.println("get(b) before insert: " + map.get(b));
        System.out.println("containsKey(b) before insert: " + map.containsKey(b));
        System.out.println("put(b, null): " + map.put(b, null));
        System.out.println("get(b) after insert: " + map.get(b));
        System.out.println("containsKey(b) after insert: " + map.containsKey(b));
        System.out.println("get(a) after insert: " + map.get(a));
        System.out.println("final size: " + map.size());
    }
}

Result

a.equals(b): true
a == b: false
put(a): null
get(alias): first
put(alias): first
size after replacement: 1
get(b) before insert: null
containsKey(b) before insert: false
put(b, null): null
get(b) after insert: null
containsKey(b) after insert: true
get(a) after insert: updated
final size: 2

`a` and `b` contain the same text but are different objects. `alias` refers to `a`, so writing through `alias` returns `first` and leaves the size at `1`. `get(b)` returns `null` both before and after insertion, but `containsKey(b)` changes from `false` to `true`, and the size grows to `2`.

Related lessons

  • Java · Lesson 17 · Advanced · Version: 2

Sources