java.util

Class IdentityHashMap<K,V>

Implemented Interfaces:
Cloneable, Map<K,V>, Serializable

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

This class provides a hashtable-backed implementation of the Map interface, but uses object identity to do its hashing. In fact, it uses object identity for comparing values, as well. It uses a linear-probe hash table, which may have faster performance than the chaining employed by HashMap.

WARNING: This is not a general purpose map. Because it uses System.identityHashCode and ==, instead of hashCode and equals, for comparison, it violated Map's general contract, and may cause undefined behavior when compared to other maps which are not IdentityHashMaps. This is designed only for the rare cases when identity semantics are needed. An example use is topology-preserving graph transformations, such as deep cloning, or as proxy object mapping such as in debugging.

This map permits null keys and values, and does not guarantee that elements will stay in the same order over time. The basic operations (get and put) take constant time, provided System.identityHashCode is decent. You can tune the behavior by specifying the expected maximum size. As more elements are added, the map may need to allocate a larger table, which can be expensive.

This implementation is unsynchronized. If you want multi-thread access to be consistent, you must synchronize it, perhaps by using Collections.synchronizedMap(new IdentityHashMap(...));. The iterators are fail-fast, meaning that a structural modification made to the map outside of an iterator's remove method cause the iterator, and in the case of the entrySet, the Map.Entry, to fail with a ConcurrentModificationException.

Since:
1.4
See Also:
System.identityHashCode(Object), Collection, Map, HashMap, TreeMap, LinkedHashMap, WeakHashMap, Serialized Form

Nested Class Summary

Nested classes/interfaces inherited from class java.util.AbstractMap<K,V>

AbstractMap.SimpleEntry, AbstractMap.SimpleImmutableEntry

Constructor Summary

IdentityHashMap()
Create a new IdentityHashMap with the default capacity (21 entries).
IdentityHashMap(extends K, V> m)
Create a new IdentityHashMap whose contents are taken from the given Map.
IdentityHashMap(int max)
Create a new IdentityHashMap with the indicated number of entries.

Method Summary

void
clear()
Remove all mappings from this map.
Object
clone()
Creates a shallow copy where keys and values are not cloned.
boolean
containsKey(Object key)
Tests whether the specified key is in this map.
boolean
containsValue(Object value)
Returns true if this HashMap contains the value.
Set>
entrySet()
Returns a "set view" of this Map's entries.
boolean
equals(Object o)
Compares two maps for equality.
V
get(Object key)
Return the value in this Map associated with the supplied key, or null if the key maps to nothing.
int
hashCode()
Returns the hashcode of this map.
boolean
isEmpty()
Returns true if there are no key-value mappings currently in this Map
Set
keySet()
Returns a "set view" of this Map's keys.
V
put(K key, V value)
Puts the supplied value into the Map, mapped by the supplied key.
void
putAll(extends K, V> m)
Copies all of the mappings from the specified map to this.
V
remove(Object key)
Removes from the HashMap and returns the value which is mapped by the supplied key.
int
size()
Returns the number of kay-value mappings currently in this Map
Collection
values()
Returns a "collection view" (or "bag view") of this Map's values.

Methods inherited from class java.util.AbstractMap<K,V>

V>> entrySet, clear, clone, containsKey, containsValue, equals, get, hashCode, isEmpty, keySet, put, putAll, remove, size, toString, values

Methods inherited from class java.lang.Object

clone, equals, extends Object> getClass, finalize, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructor Details

IdentityHashMap

public IdentityHashMap()
Create a new IdentityHashMap with the default capacity (21 entries).

IdentityHashMap

public IdentityHashMap(extends K,
                       V> m)
Create a new IdentityHashMap whose contents are taken from the given Map.
Parameters:
m - The map whose elements are to be put in this map
Throws:
NullPointerException - if m is null

IdentityHashMap

public IdentityHashMap(int max)
Create a new IdentityHashMap with the indicated number of entries. If the number of elements added to this hash map exceeds this maximum, the map will grow itself; however, that incurs a performance penalty.
Parameters:
max - initial size
Throws:
IllegalArgumentException - if max is negative

Method Details

clear

public void clear()
Remove all mappings from this map.
Specified by:
clear in interface Map<K,V>
Overrides:
clear in interface AbstractMap<K,V>

clone

public Object clone()
Creates a shallow copy where keys and values are not cloned.
Overrides:
clone in interface AbstractMap<K,V>

containsKey

public boolean containsKey(Object key)
Tests whether the specified key is in this map. Unlike normal Maps, this test uses entry == key instead of entry == null ? key == null : entry.equals(key).
Specified by:
containsKey in interface Map<K,V>
Overrides:
containsKey in interface AbstractMap<K,V>
Parameters:
key - the key to look for
Returns:
true if the key is contained in the map

containsValue

public boolean containsValue(Object value)
Returns true if this HashMap contains the value. Unlike normal maps, this test uses entry == value instead of entry == null ? value == null : entry.equals(value).
Specified by:
containsValue in interface Map<K,V>
Overrides:
containsValue in interface AbstractMap<K,V>
Parameters:
value - the value to search for in this HashMap
Returns:
true if at least one key maps to the value

entrySet

public Set> entrySet()
Returns a "set view" of this Map's entries. The set is backed by the Map, so changes in one show up in the other. The set supports element removal, but not element addition.

The semantics of this set, and of its contained entries, are different from the contract of Set and Map.Entry in order to make IdentityHashMap work. This means that while you can compare these objects between IdentityHashMaps, comparing them with regular sets or entries is likely to have undefined behavior. The entries in this set are reference-based, rather than the normal object equality. Therefore, e1.equals(e2) returns e1.getKey() == e2.getKey() && e1.getValue() == e2.getValue(), and e.hashCode() returns System.identityHashCode(e.getKey()) ^ System.identityHashCode(e.getValue()).

Note that the iterators for all three views, from keySet(), entrySet(), and values(), traverse the Map in the same sequence.

Returns:
a set view of the entries

equals

public boolean equals(Object o)
Compares two maps for equality. This returns true only if both maps have the same reference-identity comparisons. While this returns this.entrySet().equals(m.entrySet()) as specified by Map, this will not work with normal maps, since the entry set compares with == instead of .equals.
Specified by:
equals in interface Map<K,V>
Overrides:
equals in interface AbstractMap<K,V>
Parameters:
o - the object to compare to
Returns:
true if it is equal

get

public V get(Object key)
Return the value in this Map associated with the supplied key, or null if the key maps to nothing.

NOTE: Since the value could also be null, you must use containsKey to see if this key actually maps to something. Unlike normal maps, this tests for the key with entry == key instead of entry == null ? key == null : entry.equals(key).

Specified by:
get in interface Map<K,V>
Overrides:
get in interface AbstractMap<K,V>
Parameters:
key - the key for which to fetch an associated value
Returns:
what the key maps to, if present
See Also:
put(Object, Object), containsKey(Object)

hashCode

public int hashCode()
Returns the hashcode of this map. This guarantees that two IdentityHashMaps that compare with equals() will have the same hash code, but may break with comparison to normal maps since it uses System.identityHashCode() instead of hashCode().
Specified by:
hashCode in interface Map<K,V>
Overrides:
hashCode in interface AbstractMap<K,V>
Returns:
the hash code

isEmpty

public boolean isEmpty()
Returns true if there are no key-value mappings currently in this Map
Specified by:
isEmpty in interface Map<K,V>
Overrides:
isEmpty in interface AbstractMap<K,V>
Returns:
size() == 0

keySet

public Set keySet()
Returns a "set view" of this Map's keys. The set is backed by the Map, so changes in one show up in the other. The set supports element removal, but not element addition.

The semantics of this set are different from the contract of Set in order to make IdentityHashMap work. This means that while you can compare these objects between IdentityHashMaps, comparing them with regular sets is likely to have undefined behavior. The hashCode of the set is the sum of the identity hash codes, instead of the regular hashCodes, and equality is determined by reference instead of by the equals method.

Specified by:
keySet in interface Map<K,V>
Overrides:
keySet in interface AbstractMap<K,V>
Returns:
a set view of the keys

put

public V put(K key,
             V value)
Puts the supplied value into the Map, mapped by the supplied key. The value may be retrieved by any object which equals() this key. NOTE: Since the prior value could also be null, you must first use containsKey if you want to see if you are replacing the key's mapping. Unlike normal maps, this tests for the key with entry == key instead of entry == null ? key == null : entry.equals(key).
Specified by:
put in interface Map<K,V>
Overrides:
put in interface AbstractMap<K,V>
Parameters:
key - the key used to locate the value
value - the value to be stored in the HashMap
Returns:
the prior mapping of the key, or null if there was none
See Also:
get(Object)

putAll

public void putAll(extends K,
                   V> m)
Copies all of the mappings from the specified map to this. If a key is already in this map, its value is replaced.
Specified by:
putAll in interface Map<K,V>
Overrides:
putAll in interface AbstractMap<K,V>
Parameters:
m - the map to copy
Throws:
NullPointerException - if m is null

remove

public V remove(Object key)
Removes from the HashMap and returns the value which is mapped by the supplied key. If the key maps to nothing, then the HashMap remains unchanged, and null is returned. NOTE: Since the value could also be null, you must use containsKey to see if you are actually removing a mapping. Unlike normal maps, this tests for the key with entry == key instead of entry == null ? key == null : entry.equals(key).
Specified by:
remove in interface Map<K,V>
Overrides:
remove in interface AbstractMap<K,V>
Parameters:
key - the key used to locate the value to remove
Returns:
whatever the key mapped to, if present

size

public int size()
Returns the number of kay-value mappings currently in this Map
Specified by:
size in interface Map<K,V>
Overrides:
size in interface AbstractMap<K,V>
Returns:
the size

values

public Collection values()
Returns a "collection view" (or "bag view") of this Map's values. The collection is backed by the Map, so changes in one show up in the other. The collection supports element removal, but not element addition.

The semantics of this set are different from the contract of Collection in order to make IdentityHashMap work. This means that while you can compare these objects between IdentityHashMaps, comparing them with regular sets is likely to have undefined behavior. Likewise, contains and remove go by == instead of equals().

Specified by:
values in interface Map<K,V>
Overrides:
values in interface AbstractMap<K,V>
Returns:
a bag view of the values

IdentityHashMap.java -- a class providing a hashtable data structure, mapping Object --> Object, which uses object identity for hashing. Copyright (C) 2001, 2002, 2004, 2005 Free Software Foundation, Inc. This file is part of GNU Classpath. GNU Classpath is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. GNU Classpath is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with GNU Classpath; see the file COPYING. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. Linking this library statically or dynamically with other modules is making a combined work based on this library. Thus, the terms and conditions of the GNU General Public License cover the whole combination. As a special exception, the copyright holders of this library give you permission to link this library with independent modules to produce an executable, regardless of the license terms of these independent modules, and to copy and distribute the resulting executable under terms of your choice, provided that you also meet, for each linked independent module, the terms and conditions of the license of that module. An independent module is a module which is not derived from or based on this library. If you modify this library, you may extend this exception to your version of the library, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version.