< prev index next > src/java.base/share/classes/java/util/IdentityHashMap.java
Print this page
/*
! * Copyright (c) 2000, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
/*
! * Copyright (c) 2000, 2026, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
import java.util.function.Consumer;
import jdk.internal.access.SharedSecrets;
/**
* This class implements the {@code Map} interface with a hash table, using
! * reference-equality in place of object-equality when comparing keys (and
! * values). In other words, in an {@code IdentityHashMap}, two keys
* {@code k1} and {@code k2} are considered equal if and only if
* {@code (k1==k2)}. (In normal {@code Map} implementations (like
* {@code HashMap}) two keys {@code k1} and {@code k2} are considered equal
* if and only if {@code (k1==null ? k2==null : k1.equals(k2))}.)
*
* <p><b>This class is <i>not</i> a general-purpose {@code Map}
* implementation! While this class implements the {@code Map} interface, it
* intentionally violates {@code Map's} general contract, which mandates the
* use of the {@code equals} method when comparing objects. This class is
! * designed for use only in the rare cases wherein reference-equality
! * semantics are required.</b>
*
! * <p>The view collections of this map also have reference-equality semantics
! * for their elements. See the {@link keySet() keySet}, {@link values() values},
! * and {@link entrySet() entrySet} methods for further information.
*
* <p>A typical use of this class is <i>topology-preserving object graph
* transformations</i>, such as serialization or deep-copying. To perform such
* a transformation, a program must maintain a "node table" that keeps track
* of all the object references that have already been processed. The node
import java.util.function.Consumer;
import jdk.internal.access.SharedSecrets;
/**
* This class implements the {@code Map} interface with a hash table, using
! * {@code == } in place of {@code .equals()} equality when comparing keys (and values).
! * In other words, in an {@code IdentityHashMap}, two keys
* {@code k1} and {@code k2} are considered equal if and only if
* {@code (k1==k2)}. (In normal {@code Map} implementations (like
* {@code HashMap}) two keys {@code k1} and {@code k2} are considered equal
* if and only if {@code (k1==null ? k2==null : k1.equals(k2))}.)
*
* <p><b>This class is <i>not</i> a general-purpose {@code Map}
* implementation! While this class implements the {@code Map} interface, it
* intentionally violates {@code Map's} general contract, which mandates the
* use of the {@code equals} method when comparing objects. This class is
! * designed for use only in the rare cases wherein {@code == } semantics are required.
! * </b>
+ * <div class="preview-block">
+ * <div class="preview-comment">
+ * When preview features are enabled, keys and values may be identity objects,
+ * value objects or null.
+ * For value classes, two value objects are state-wise equivalent if they are instances
+ * of the same class and the values of their instance fields are the same.
+ * </div>
+ * </div>
*
! * <p>The view collections of this map also have {@code == } equality semantics
! * for their elements. See the {@link #keySet() keySet}, {@link #values() values},
! * and {@link #entrySet() entrySet} methods for further information.
*
* <p>A typical use of this class is <i>topology-preserving object graph
* transformations</i>, such as serialization or deep-copying. To perform such
* a transformation, a program must maintain a "node table" that keeps track
* of all the object references that have already been processed. The node
/**
* Returns the appropriate capacity for the given expected maximum size.
* Returns the smallest power of two between MINIMUM_CAPACITY and
* MAXIMUM_CAPACITY, inclusive, that is greater than (3 *
! * expectedMaxSize)/2, if such a number exists. Otherwise returns
* MAXIMUM_CAPACITY.
*/
private static int capacity(int expectedMaxSize) {
// assert expectedMaxSize >= 0;
return
/**
* Returns the appropriate capacity for the given expected maximum size.
* Returns the smallest power of two between MINIMUM_CAPACITY and
* MAXIMUM_CAPACITY, inclusive, that is greater than (3 *
! * expectedMaxSize)/2, if such a number exists. Otherwise, returns
* MAXIMUM_CAPACITY.
*/
private static int capacity(int expectedMaxSize) {
// assert expectedMaxSize >= 0;
return
}
}
/**
* Associates the specified value with the specified key in this identity
! * hash map. If this map already {@link containsKey(Object) contains}
* a mapping for the key, the old value is replaced, otherwise, a new mapping
* is inserted into this map.
*
* @param key the key with which the specified value is to be associated
* @param value the value to be associated with the specified key
}
}
/**
* Associates the specified value with the specified key in this identity
! * hash map. If this map already {@link #containsKey(Object) contains}
* a mapping for the key, the old value is replaced, otherwise, a new mapping
* is inserted into this map.
*
* @param key the key with which the specified value is to be associated
* @param value the value to be associated with the specified key
}
/**
* Copies all of the mappings from the specified map to this map.
* For each mapping in the specified map, if this map already
! * {@link containsKey(Object) contains} a mapping for the key,
* its value is replaced with the value from the specified map;
* otherwise, a new mapping is inserted into this map.
*
* @param m mappings to be stored in this map
* @throws NullPointerException if the specified map is null
}
/**
* Copies all of the mappings from the specified map to this map.
* For each mapping in the specified map, if this map already
! * {@link #containsKey(Object) contains} a mapping for the key,
* its value is replaced with the value from the specified map;
* otherwise, a new mapping is inserted into this map.
*
* @param m mappings to be stored in this map
* @throws NullPointerException if the specified map is null
* Compares the specified object with this map for equality. Returns
* {@code true} if the given object is also a map and the two maps
* represent identical object-reference mappings. More formally, this
* map is equal to another map {@code m} if and only if
* {@code this.entrySet().equals(m.entrySet())}. See the
! * {@link entrySet() entrySet} method for the specification of equality
* of this map's entries.
*
* <p><b>Owing to the reference-equality-based semantics of this map it is
* possible that the symmetry and transitivity requirements of the
* {@code Object.equals} contract may be violated if this map is compared
* Compares the specified object with this map for equality. Returns
* {@code true} if the given object is also a map and the two maps
* represent identical object-reference mappings. More formally, this
* map is equal to another map {@code m} if and only if
* {@code this.entrySet().equals(m.entrySet())}. See the
! * {@link #entrySet() entrySet} method for the specification of equality
* of this map's entries.
*
* <p><b>Owing to the reference-equality-based semantics of this map it is
* possible that the symmetry and transitivity requirements of the
* {@code Object.equals} contract may be violated if this map is compared
}
/**
* Returns the hash code value for this map. The hash code of a map is
* defined to be the sum of the hash codes of each entry of this map.
! * See the {@link entrySet() entrySet} method for a specification of the
* hash code of this map's entries.
*
* <p>This specification ensures that {@code m1.equals(m2)}
* implies that {@code m1.hashCode()==m2.hashCode()} for any two
* {@code IdentityHashMap} instances {@code m1} and {@code m2}, as
}
/**
* Returns the hash code value for this map. The hash code of a map is
* defined to be the sum of the hash codes of each entry of this map.
! * See the {@link #entrySet() entrySet} method for a specification of the
* hash code of this map's entries.
*
* <p>This specification ensures that {@code m1.equals(m2)}
* implies that {@code m1.hashCode()==m2.hashCode()} for any two
* {@code IdentityHashMap} instances {@code m1} and {@code m2}, as
}
}
/**
* Similar form as array-based Spliterators, but skips blank elements,
! * and guestimates size as decreasing by half per split.
*/
static class IdentityHashMapSpliterator<K,V> {
final IdentityHashMap<K,V> map;
int index; // current index, modified on advance/split
int fence; // -1 until first use; then one past last index
}
}
/**
* Similar form as array-based Spliterators, but skips blank elements,
! * and guesstimates size as decreasing by half per split.
*/
static class IdentityHashMapSpliterator<K,V> {
final IdentityHashMap<K,V> map;
int index; // current index, modified on advance/split
int fence; // -1 until first use; then one past last index
< prev index next >