Map---IdentityHashMap

发布时间 2023-11-15 14:49:24作者: anpeiyong

概述

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

IdentityHashMap的key比较使用 key1==key2 {HashMap使用key的Hash值相等 && (key1==key2 || key1.equals(key2))};

 

This class is <i>not</i> a general-purpose <tt>Map</tt> implementation!
While this class implements the <tt>Map</tt> interface, it intentionally violates <tt>Map's</tt> general contract, which mandates the use of the <tt>equals</tt> 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 <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 table must not equate distinct objects even if they happen to be equal.
Another typical use of this class is to maintain <i>proxy objects</i>.
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 <tt>null</tt> values and the <tt>null</tt> 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.

IdentityHashMap允许key=null,value=null

 

This class provides constant-time performance for the basic operations (<tt>get</tt> and <tt>put</tt>), assuming the system identity hash function ({@link System#identityHashCode(Object)}) disperses elements properly among the buckets.

IdentityHashMap的get、Put花费O(1)时间复杂度,因为Hash函数将元素分散在buckets;

 

This class has one tuning parameter (which affects performance but not semantics): <i>expected maximum size</i>.
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.

IdentityHashMap有个参数expected maximum size;

 

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.

如果IdentityHashMap的size超过maximum,buckets将会增长;

buckets的增长是开销昂贵;

 

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 <i>must</i> 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 {@link Collections#synchronizedMap Collections.synchronizedMap} method.

 

IdentityHashMap是线程非同步的;

可以使用Collections.synchronizedMap;

 

The iterators returned by the <tt>iterator</tt> method of the collections returned by all of this class's "collection view methods" are <i>fail-fast</i>: if the map is structurally modified at any time after the iterator is created, in any way except through the iterator's own <tt>remove</tt> method, the iterator will throw a {@link 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.

IdentityHashMap的iterator是fail-fast

 

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 <tt>ConcurrentModificationException</tt> on a best-effort basis.
Therefore, it would be wrong to write a program that depended on this exception for its correctness: <i>fail-fast iterators should be used only to detect bugs.

 

Implementation note: This is a simple <i>linear-probe</i> 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 {@link HashMap} (which uses <i>chaining</i> rather than linear-probing).

 

链路

put(K key, V value)

// java.util.IdentityHashMap.put
    public V put(K key, V value) {
        final Object k = maskNull(key);

        retryAfterResize: for (;;) {
            final Object[] tab = table;
            final int len = tab.length;
            int i = hash(k, len);

            for (Object item; (item = tab[i]) != null; i = nextKeyIndex(i, len)) {                  // 索引位item!=null
                if (item == k) {                                                                    // item == key -> value替换oldValue
                    @SuppressWarnings("unchecked")
                    V oldValue = (V) tab[i + 1];
                    tab[i + 1] = value;
                    return oldValue;
                }
            }

            final int s = size + 1;
            // Use optimized form of 3 * s.
            // Next capacity is len, 2 * current capacity.
            if (s + (s << 1) > len && resize(len))
                continue retryAfterResize;

            modCount++;
            tab[i] = k;
            tab[i + 1] = value;
            size = s;
            return null;
        }
    }

  

get(Object key)

// java.util.IdentityHashMap.get
    public V get(Object key) {
        Object k = maskNull(key);
        Object[] tab = table;
        int len = tab.length;
        int i = hash(k, len);
        while (true) {
            Object item = tab[i];
            if (item == k)                                                                          // 如果索引位的item==key -> 返回item
                return (V) tab[i + 1];
            if (item == null)
                return null;
            i = nextKeyIndex(i, len);
        }
    }

  

map.keySet().iterator()

// java.util.IdentityHashMap.keySet
    public Set<K> keySet() {
        Set<K> ks = keySet;
        if (ks == null) {
            ks = new KeySet();
            keySet = ks;
        }
        return ks;
    }

    // java.util.IdentityHashMap.KeySet
    public class IdentityHashMap<K,V> extends AbstractMap<K,V> implements Map<K,V>, java.io.Serializable, Cloneable{
        private class KeySet extends AbstractSet<K> {

        }
    }

    // java.util.IdentityHashMap.KeySet.iterator
    public class IdentityHashMap<K,V> extends AbstractMap<K,V> implements Map<K,V>, java.io.Serializable, Cloneable{
        private class KeySet extends AbstractSet<K> {
            public Iterator<K> iterator() {
                return new KeyIterator();
            }
        }
    }

    // java.util.IdentityHashMap.KeyIterator
    public class IdentityHashMap<K,V> extends AbstractMap<K,V> implements Map<K,V>, java.io.Serializable, Cloneable{
        private class KeyIterator extends IdentityHashMapIterator<K> {
            public K next() {
                return (K) unmaskNull(traversalTable[nextIndex()]);
            }
        }
    }

    // java.util.IdentityHashMap.IdentityHashMapIterator
    public class IdentityHashMap<K,V> extends AbstractMap<K,V> implements Map<K,V>, java.io.Serializable, Cloneable{
        private abstract class IdentityHashMapIterator<T> implements Iterator<T> {
            ...
        }
    }

  

map.entrySet().iterator()

// java.util.IdentityHashMap.entrySet
    public Set<Map.Entry<K,V>> entrySet() {
        Set<Map.Entry<K,V>> es = entrySet;
        if (es != null)
            return es;
        else
            return entrySet = new EntrySet();
    }

    // java.util.IdentityHashMap.EntrySet
    public class IdentityHashMap<K,V> extends AbstractMap<K,V> implements Map<K,V>, java.io.Serializable, Cloneable{
        private class EntrySet extends AbstractSet<Map.Entry<K,V>> {

        }
    }

    // java.util.IdentityHashMap.EntrySet.iterator
    public class IdentityHashMap<K,V> extends AbstractMap<K,V> implements Map<K,V>, java.io.Serializable, Cloneable{
        private class EntrySet extends AbstractSet<Map.Entry<K,V>> {
            public Iterator<Map.Entry<K,V>> iterator() {
                return new EntryIterator();
            }
        }
    }

    // java.util.IdentityHashMap.EntryIterator
    public class IdentityHashMap<K,V> extends AbstractMap<K,V> implements Map<K,V>, java.io.Serializable, Cloneable{
        private class EntryIterator extends IdentityHashMapIterator<Entry<K,V>>{
            public Map.Entry<K,V> next() {
                lastReturnedEntry = new Entry(nextIndex());
                return lastReturnedEntry;
            }

            public void remove() {
                lastReturnedIndex =
                        ((null == lastReturnedEntry) ? -1 : lastReturnedEntry.index);
                super.remove();
                lastReturnedEntry.index = lastReturnedIndex;
                lastReturnedEntry = null;
            }
        }
    }

    // java.util.IdentityHashMap.IdentityHashMapIterator
    public class IdentityHashMap<K,V> extends AbstractMap<K,V> implements Map<K,V>, java.io.Serializable, Cloneable{
        private abstract class IdentityHashMapIterator<T> implements Iterator<T> {
            public boolean hasNext() {
                Object[] tab = traversalTable;
                for (int i = index; i < tab.length; i+=2) {
                    Object key = tab[i];
                    if (key != null) {
                        index = i;
                        return indexValid = true;
                    }
                }
                index = tab.length;
                return false;
            }

            protected int nextIndex() {
                if (modCount != expectedModCount)
                    throw new ConcurrentModificationException();
                if (!indexValid && !hasNext())
                    throw new NoSuchElementException();

                indexValid = false;
                lastReturnedIndex = index;
                index += 2;
                return lastReturnedIndex;
            }
        }
    }