# Print output for @column tags ?>
Developers

- Design
- Develop
- Distribute

Developer Console

## Most visited

## Recently visited

## Results for

Summary: Ctors | Methods | Inherited Methods

# IdentityHashMap

`public class IdentityHashMap`
`` `extends AbstractMap<K, V>` `implements Map<K, V>, Serializable, Cloneable`

java.lang.Object
↳ | java.util.AbstractMap<K, V>
| ↳ | java.util.IdentityHashMap<K, V>

This class implements the `Map` interface with a hash table, using reference-equality in place of object-equality when comparing keys (and values). In other words, in an `IdentityHashMap`, two keys `k1` and `k2` are considered equal if and only if `(k1==k2)`. (In normal `Map` implementations (like `HashMap`) two keys `k1` and `k2` are considered equal if and only if `(k1==null ? k2==null : k1.equals(k2))`.)

This class is not a general-purpose `Map` implementation! While this class implements the `Map` interface, it intentionally violates `Map's` general contract, which mandates the use of the `equals` method when comparing objects. This class is designed for use only in the rare cases wherein reference-equality semantics are required.

A typical use of this class is topology-preserving object graph transformations, 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 table must not equate distinct objects even if they happen to be equal. Another typical use of this class is to maintain proxy objects. For example, a debugging facility might wish to maintain a proxy object for each object in the program being debugged.

This class provides all of the optional map operations, and permits `null` values and the `null` key. This class makes no guarantees as to the order of the map; in particular, it does not guarantee that the order will remain constant over time.

This class provides constant-time performance for the basic operations (`get` and `put`), assuming the system identity hash function (`System#identityHashCode(Object)`) disperses elements properly among the buckets.

This class has one tuning parameter (which affects performance but not semantics): expected maximum size. This parameter is the maximum number of key-value mappings that the map is expected to hold. Internally, this parameter is used to determine the number of buckets initially comprising the hash table. The precise relationship between the expected maximum size and the number of buckets is unspecified.

If the size of the map (the number of key-value mappings) sufficiently exceeds the expected maximum size, the number of buckets is increased. Increasing the number of buckets ("rehashing") may be fairly expensive, so it pays to create identity hash maps with a sufficiently large expected maximum size. On the other hand, iteration over collection views requires time proportional to the number of buckets in the hash table, so it pays not to set the expected maximum size too high if you are especially concerned with iteration performance or memory usage.

Note that this implementation is not synchronized. If multiple threads access an identity hash map concurrently, and at least one of the threads modifies the map structurally, it must be synchronized externally. (A structural modification is any operation that adds or deletes one or more mappings; merely changing the value associated with a key that an instance already contains is not a structural modification.) This is typically accomplished by synchronizing on some object that naturally encapsulates the map. If no such object exists, the map should be "wrapped" using the `Collections.synchronizedMap` method. This is best done at creation time, to prevent accidental unsynchronized access to the map:

       Map m = Collections.synchronizedMap(new IdentityHashMap(...));

The iterators returned by the `iterator` method of the collections returned by all of this class's "collection view methods" are fail-fast: if the map is structurally modified at any time after the iterator is created, in any way except through the iterator's own `remove` method, the iterator will throw a `ConcurrentModificationException`. Thus, in the face of concurrent modification, the iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future.

Note that the fail-fast behavior of an iterator cannot be guaranteed as it is, generally speaking, impossible to make any hard guarantees in the presence of unsynchronized concurrent modification. Fail-fast iterators throw `ConcurrentModificationException` on a best-effort basis. Therefore, it would be wrong to write a program that depended on this exception for its correctness: fail-fast iterators should be used only to detect bugs.

Implementation note: This is a simple linear-probe hash table, as described for example in texts by Sedgewick and Knuth. The array alternates holding keys and values. (This has better locality for large tables than does using separate arrays.) For many JRE implementations and operation mixes, this class will yield better performance than `HashMap` (which uses chaining rather than linear-probing).

This class is a member of the Java Collections Framework.

See also:

- `System.identityHashCode(Object)`
- `Object.hashCode()`
- `Collection`
- `Map`
- `HashMap`
- `TreeMap`

## Summary

### Public constructors
`IdentityHashMap()` Constructs a new, empty identity hash map with a default expected maximum size (21).
`IdentityHashMap(int expectedMaxSize)` Constructs a new, empty map with the specified expected maximum size.
`IdentityHashMap(Map<? extends K, ? extends V> m)` Constructs a new identity hash map containing the keys-value mappings in the specified map.

### Public methods
`void` | `clear()` Removes all of the mappings from this map.
`Object` | `clone()` Returns a shallow copy of this identity hash map: the keys and values themselves are not cloned.
`boolean` | `containsKey(Object key)` Tests whether the specified object reference is a key in this identity hash map.
`boolean` | `containsValue(Object value)` Tests whether the specified object reference is a value in this identity hash map.
`Set<Entry<K, V>>` | `entrySet()` Returns a `Set` view of the mappings contained in this map.
`boolean` | `equals(Object o)` Compares the specified object with this map for equality.
`void` | `forEach(BiConsumer<? super K, ? super V> action)` Performs the given action for each entry in this map until all entries have been processed or the action throws an exception.
`V` | `get(Object key)` Returns the value to which the specified key is mapped, or `null` if this map contains no mapping for the key.
`int` | `hashCode()` Returns the hash code value for this map.
`boolean` | `isEmpty()` Returns `true` if this identity hash map contains no key-value mappings.
`Set<K>` | `keySet()` Returns an identity-based set view of the keys contained in this map.
`V` | `put(K key, V value)` Associates the specified value with the specified key in this identity hash map.
`void` | `putAll(Map<? extends K, ? extends V> m)` Copies all of the mappings from the specified map to this map.
`V` | `remove(Object key)` Removes the mapping for this key from this map if present.
`void` | `replaceAll(BiFunction<? super K, ? super V, ? extends V> function)` Replaces each entry's value with the result of invoking the given function on that entry until all entries have been processed or the function throws an exception.
`int` | `size()` Returns the number of key-value mappings in this identity hash map.
`Collection<V>` | `values()` Returns a `Collection` view of the values contained in this map.

### Inherited methods
From class `java.util.AbstractMap`
`void` | `clear()` Removes all of the mappings from this map (optional operation).
`Object` | `clone()` Returns a shallow copy of this `AbstractMap` instance: the keys and values themselves are not cloned.
`boolean` | `containsKey(Object key)` Returns `true` if this map contains a mapping for the specified key.
`boolean` | `containsValue(Object value)` Returns `true` if this map maps one or more keys to the specified value.
`abstract Set<Entry<K, V>>` | `entrySet()` Returns a `Set` view of the mappings contained in this map.
`boolean` | `equals(Object o)` Compares the specified object with this map for equality.
`V` | `get(Object key)` Returns the value to which the specified key is mapped, or `null` if this map contains no mapping for the key.
`int` | `hashCode()` Returns the hash code value for this map.
`boolean` | `isEmpty()` Returns `true` if this map contains no key-value mappings.
`Set<K>` | `keySet()` Returns a `Set` view of the keys contained in this map.
`V` | `put(K key, V value)` Associates the specified value with the specified key in this map (optional operation).
`void` | `putAll(Map<? extends K, ? extends V> m)` Copies all of the mappings from the specified map to this map (optional operation).
`V` | `remove(Object key)` Removes the mapping for a key from this map if it is present (optional operation).
`int` | `size()` Returns the number of key-value mappings in this map.
`String` | `toString()` Returns a string representation of this map.
`Collection<V>` | `values()` Returns a `Collection` view of the values contained in this map.
From class `java.lang.Object`
`Object` | `clone()` Creates and returns a copy of this object.
`boolean` | `equals(Object obj)` Indicates whether some other object is "equal to" this one.
`void` | `finalize()` Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.
`final Class<?>` | `getClass()` Returns the runtime class of this `Object`.
`int` | `hashCode()` Returns a hash code value for the object.
`final void` | `notify()` Wakes up a single thread that is waiting on this object's monitor.
`final void` | `notifyAll()` Wakes up all threads that are waiting on this object's monitor.
`String` | `toString()` Returns a string representation of the object.
`final void` | `wait(long timeout, int nanos)` Causes the current thread to wait until another thread invokes the `notify()` method or the `notifyAll()` method for this object, or some other thread interrupts the current thread, or a certain amount of real time has elapsed.
`final void` | `wait(long timeout)` Causes the current thread to wait until either another thread invokes the `notify()` method or the `notifyAll()` method for this object, or a specified amount of time has elapsed.
`final void` | `wait()` Causes the current thread to wait until another thread invokes the `notify()` method or the `notifyAll()` method for this object.
From interface `java.util.Map`
`abstract void` | `clear()` Removes all of the mappings from this map (optional operation).
`default V` | `compute(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction)` Attempts to compute a mapping for the specified key and its current mapped value (or `null` if there is no current mapping).
`default V` | `computeIfAbsent(K key, Function<? super K, ? extends V> mappingFunction)` If the specified key is not already associated with a value (or is mapped to `null`), attempts to compute its value using the given mapping function and enters it into this map unless `null`.
`default V` | `computeIfPresent(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction)` If the value for the specified key is present and non-null, attempts to compute a new mapping given the key and its current mapped value.
`abstract boolean` | `containsKey(Object key)` Returns `true` if this map contains a mapping for the specified key.
`abstract boolean` | `containsValue(Object value)` Returns `true` if this map maps one or more keys to the specified value.
`static <K, V> Map<K, V>` | `copyOf(Map<? extends K, ? extends V> map)` Returns an unmodifiable Map containing the entries of the given Map.
`static <K, V> Entry<K, V>` | `entry(K k, V v)` Returns an immutable `Entry` containing the given key and value.
`abstract Set<Entry<K, V>>` | `entrySet()` Returns a `Set` view of the mappings contained in this map.
`abstract boolean` | `equals(Object o)` Compares the specified object with this map for equality.
`default void` | `forEach(BiConsumer<? super K, ? super V> action)` Performs the given action for each entry in this map until all entries have been processed or the action throws an exception.
`abstract V` | `get(Object key)` Returns the value to which the specified key is mapped, or `null` if this map contains no mapping for the key.
`default V` | `getOrDefault(Object key, V defaultValue)` Returns the value to which the specified key is mapped, or `defaultValue` if this map contains no mapping for the key.
`abstract int` | `hashCode()` Returns the hash code value for this map.
`abstract boolean` | `isEmpty()` Returns `true` if this map contains no key-value mappings.
`abstract Set<K>` | `keySet()` Returns a `Set` view of the keys contained in this map.
`default V` | `merge(K key, V value, BiFunction<? super V, ? super V, ? extends V> remappingFunction)` If the specified key is not already associated with a value or is associated with null, associates it with the given non-null value.
`static <K, V> Map<K, V>` | `of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5)` Returns an immutable map containing five mappings.
`static <K, V> Map<K, V>` | `of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4)` Returns an immutable map containing four mappings.
`static <K, V> Map<K, V>` | `of(K k1, V v1)` Returns an immutable map containing a single mapping.
`static <K, V> Map<K, V>` | `of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8)` Returns an immutable map containing eight mappings.
`static <K, V> Map<K, V>` | `of(K k1, V v1, K k2, V v2)` Returns an immutable map containing two mappings.
`static <K, V> Map<K, V>` | `of(K k1, V v1, K k2, V v2, K k3, V v3)` Returns an immutable map containing three mappings.
`static <K, V> Map<K, V>` | `of()` Returns an immutable map containing zero mappings.
`static <K, V> Map<K, V>` | `of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8, K k9, V v9)` Returns an immutable map containing nine mappings.
`static <K, V> Map<K, V>` | `of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6)` Returns an immutable map containing six mappings.
`static <K, V> Map<K, V>` | `of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7)` Returns an immutable map containing seven mappings.
`static <K, V> Map<K, V>` | `of(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5, K k6, V v6, K k7, V v7, K k8, V v8, K k9, V v9, K k10, V v10)` Returns an immutable map containing ten mappings.
`static <K, V> Map<K, V>` | `ofEntries(Entry...<? extends K, ? extends V> entries)` Returns an immutable map containing keys and values extracted from the given entries.
`abstract V` | `put(K key, V value)` Associates the specified value with the specified key in this map (optional operation).
`abstract void` | `putAll(Map<? extends K, ? extends V> m)` Copies all of the mappings from the specified map to this map (optional operation).
`default V` | `putIfAbsent(K key, V value)` If the specified key is not already associated with a value (or is mapped to `null`) associates it with the given value and returns `null`, else returns the current value.
`default boolean` | `remove(Object key, Object value)` Removes the entry for the specified key only if it is currently mapped to the specified value.
`abstract V` | `remove(Object key)` Removes the mapping for a key from this map if it is present (optional operation).
`default boolean` | `replace(K key, V oldValue, V newValue)` Replaces the entry for the specified key only if currently mapped to the specified value.
`default V` | `replace(K key, V value)` Replaces the entry for the specified key only if it is currently mapped to some value.
`default void` | `replaceAll(BiFunction<? super K, ? super V, ? extends V> function)` Replaces each entry's value with the result of invoking the given function on that entry until all entries have been processed or the function throws an exception.
`abstract int` | `size()` Returns the number of key-value mappings in this map.
`abstract Collection<V>` | `values()` Returns a `Collection` view of the values contained in this map.

## Public constructors

### IdentityHashMap

    public IdentityHashMap ()

Constructs a new, empty identity hash map with a default expected maximum size (21).

### IdentityHashMap

    public IdentityHashMap (int expectedMaxSize)

Constructs a new, empty map with the specified expected maximum size. Putting more than the expected number of key-value mappings into the map may cause the internal data structure to grow, which may be somewhat time-consuming.

Parameters
`expectedMaxSize` | `int`: the expected maximum size of the map

Throws
`IllegalArgumentException` | if `expectedMaxSize` is negative

### IdentityHashMap

    public IdentityHashMap (Map<? extends K, ? extends V> m)

Constructs a new identity hash map containing the keys-value mappings in the specified map.

Parameters
`m` | `Map`: the map whose mappings are to be placed into this map

Throws
`NullPointerException` | if the specified map is null

## Public methods

### clear

    public void clear ()

Removes all of the mappings from this map. The map will be empty after this call returns.

### clone

    public Object clone ()

Returns a shallow copy of this identity hash map: the keys and values themselves are not cloned.

Returns
`Object` | a shallow copy of this map

### containsKey

    public boolean containsKey (Object key)

Tests whether the specified object reference is a key in this identity hash map.

Parameters
`key` | `Object`: possible key

Returns
`boolean` | `true` if the specified object reference is a key in this map

See also:

- `containsValue(Object)`

### containsValue

    public boolean containsValue (Object value)

Tests whether the specified object reference is a value in this identity hash map.

Parameters
`value` | `Object`: value whose presence in this map is to be tested

Returns
`boolean` | `true` if this map maps one or more keys to the specified object reference

See also:

- `containsKey(Object)`

### entrySet

    public Set<Entry<K, V>> entrySet ()

Returns a `Set` view of the mappings contained in this map. Each element in the returned set is a reference-equality-based `Map.Entry`. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa. If the map is modified while an iteration over the set is in progress, the results of the iteration are undefined. The set supports element removal, which removes the corresponding mapping from the map, via the `Iterator.remove`, `Set.remove`, `removeAll`, `retainAll` and `clear` methods. It does not support the `add` or `addAll` methods.

Like the backing map, the `Map.Entry` objects in the set returned by this method define key and value equality as reference-equality rather than object-equality. This affects the behavior of the `equals` and `hashCode` methods of these `Map.Entry` objects. A reference-equality based `Map.Entry e` is equal to an object `o` if and only if `o` is a `Map.Entry` and `e.getKey()==o.getKey() && e.getValue()==o.getValue()`. To accommodate these equals semantics, the `hashCode` method returns `System.identityHashCode(e.getKey()) ^ System.identityHashCode(e.getValue())`.

Owing to the reference-equality-based semantics of the `Map.Entry` instances in the set returned by this method, it is possible that the symmetry and transitivity requirements of the `Object#equals(Object)` contract may be violated if any of the entries in the set is compared to a normal map entry, or if the set returned by this method is compared to a set of normal map entries (such as would be returned by a call to this method on a normal map). However, the `Object.equals` contract is guaranteed to hold among identity-based map entries, and among sets of such entries.

Returns
`Set<Entry<K, V>>` | a set view of the identity-mappings contained in this map

### equals

    public boolean equals (Object o)

Compares the specified object with this map for equality. Returns `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 `m` if and only if `this.entrySet().equals(m.entrySet())`.

Owing to the reference-equality-based semantics of this map it is possible that the symmetry and transitivity requirements of the `Object.equals` contract may be violated if this map is compared to a normal map. However, the `Object.equals` contract is guaranteed to hold among `IdentityHashMap` instances.

Parameters
`o` | `Object`: object to be compared for equality with this map

Returns
`boolean` | `true` if the specified object is equal to this map

See also:

- `Object.equals(Object)`

### forEach

    public void forEach (BiConsumer<? super K, ? super V> action)

Performs the given action for each entry in this map until all entries have been processed or the action throws an exception. Unless otherwise specified by the implementing class, actions are performed in the order of entry set iteration (if an iteration order is specified.) Exceptions thrown by the action are relayed to the caller.

Parameters
`action` | `BiConsumer`: The action to be performed for each entry

### get

    public V get (Object key)

Returns the value to which the specified key is mapped, or `null` if this map contains no mapping for the key.

More formally, if this map contains a mapping from a key `k` to a value `v` such that `(key == k)`, then this method returns `v`; otherwise it returns `null`. (There can be at most one such mapping.)

A return value of `null` does not necessarily indicate that the map contains no mapping for the key; it's also possible that the map explicitly maps the key to `null`. The `containsKey` operation may be used to distinguish these two cases.

Parameters
`key` | `Object`: the key whose associated value is to be returned

Returns
`V` | the value to which the specified key is mapped, or `null` if this map contains no mapping for the key

See also:

- `put(Object, Object)`

### hashCode

    public int hashCode ()

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 in the map's `entrySet()` view. This ensures that `m1.equals(m2)` implies that `m1.hashCode()==m2.hashCode()` for any two `IdentityHashMap` instances `m1` and `m2`, as required by the general contract of `Object#hashCode`.

Owing to the reference-equality-based semantics of the `Map.Entry` instances in the set returned by this map's `entrySet` method, it is possible that the contractual requirement of `Object.hashCode` mentioned in the previous paragraph will be violated if one of the two objects being compared is an `IdentityHashMap` instance and the other is a normal map.

Returns
`int` | the hash code value for this map

See also:

- `Object.equals(Object)`
- `equals(Object)`

### isEmpty

    public boolean isEmpty ()

Returns `true` if this identity hash map contains no key-value mappings.

Returns
`boolean` | `true` if this identity hash map contains no key-value mappings

### keySet

    public Set<K> keySet ()

Returns an identity-based set view of the keys contained in this map. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa. If the map is modified while an iteration over the set is in progress, the results of the iteration are undefined. The set supports element removal, which removes the corresponding mapping from the map, via the `Iterator.remove`, `Set.remove`, `removeAll`, `retainAll`, and `clear` methods. It does not support the `add` or `addAll` methods.

While the object returned by this method implements the `Set` interface, it does not obey `Set's` general contract. Like its backing map, the set returned by this method defines element equality as reference-equality rather than object-equality. This affects the behavior of its `contains`, `remove`, `containsAll`, `equals`, and `hashCode` methods.

The `equals` method of the returned set returns `true` only if the specified object is a set containing exactly the same object references as the returned set. The symmetry and transitivity requirements of the `Object.equals` contract may be violated if the set returned by this method is compared to a normal set. However, the `Object.equals` contract is guaranteed to hold among sets returned by this method.

The `hashCode` method of the returned set returns the sum of the identity hashcodes of the elements in the set, rather than the sum of their hashcodes. This is mandated by the change in the semantics of the `equals` method, in order to enforce the general contract of the `Object.hashCode` method among sets returned by this method.

Returns
`Set<K>` | an identity-based set view of the keys contained in this map

See also:

- `Object.equals(Object)`
- `System.identityHashCode(Object)`

### put

    public V put (K key,
                    V value)

Associates the specified value with the specified key in this identity hash map. If the map previously contained a mapping for the key, the old value is replaced.

Parameters
`key` | `K`: the key with which the specified value is to be associated
`value` | `V`: the value to be associated with the specified key

Returns
`V` | the previous value associated with `key`, or `null` if there was no mapping for `key`. (A `null` return can also indicate that the map previously associated `null` with `key`.)

See also:

- `Object.equals(Object)`
- `get(Object)`
- `containsKey(Object)`

### putAll

    public void putAll (Map<? extends K, ? extends V> m)

Copies all of the mappings from the specified map to this map. These mappings will replace any mappings that this map had for any of the keys currently in the specified map.

Parameters
`m` | `Map`: mappings to be stored in this map

Throws
`NullPointerException` | if the specified map is null

### remove

    public V remove (Object key)

Removes the mapping for this key from this map if present.

Parameters
`key` | `Object`: key whose mapping is to be removed from the map

Returns
`V` | the previous value associated with `key`, or `null` if there was no mapping for `key`. (A `null` return can also indicate that the map previously associated `null` with `key`.)

### replaceAll

    public void replaceAll (BiFunction<? super K, ? super V, ? extends V> function)

Replaces each entry's value with the result of invoking the given function on that entry until all entries have been processed or the function throws an exception. Exceptions thrown by the function are relayed to the caller.

Parameters
`function` | `BiFunction`: the function to apply to each entry

### size

    public int size ()

Returns the number of key-value mappings in this identity hash map.

Returns
`int` | the number of key-value mappings in this map

### values

    public Collection<V> values ()

Returns a `Collection` view of the values contained in this map. The collection is backed by the map, so changes to the map are reflected in the collection, and vice-versa. If the map is modified while an iteration over the collection is in progress, the results of the iteration are undefined. The collection supports element removal, which removes the corresponding mapping from the map, via the `Iterator.remove`, `Collection.remove`, `removeAll`, `retainAll` and `clear` methods. It does not support the `add` or `addAll` methods.

While the object returned by this method implements the `Collection` interface, it does not obey `Collection's` general contract. Like its backing map, the collection returned by this method defines element equality as reference-equality rather than object-equality. This affects the behavior of its `contains`, `remove` and `containsAll` methods.

Returns
`Collection<V>` | a collection view of the values contained in this map

Full name *
Email address *
Company / developer name
One of your Play Store app URLs *
Which best describes your business:*
Apps
Games
Apps & Games
*
*