Map---IdentityHashMap

总结

  Java 中的一种特殊类型的哈希表,它基于对象的引用(即对象的内存地址)而不是对象的内容来进行键值对的存储和查找。这意味着在 IdentityHashMap 中,两个对象即使内容相同,但如果它们不是同一个对象实例(即内存地址不同),也会被视为不同的键。

  

基于引用比较
  IdentityHashMap 使用 == 运算符而不是 equals 方法来比较键。
  这意味着只有当两个对象是同一个实例时,它们才会被认为是相等的。
不使用 hashCode 方法:
  IdentityHashMap 使用对象的系统标识符(通常是对象的内存地址)来计算哈希值,而不是对象的 hashCode 方法。
线程不安全
  IdentityHashMap 不是线程安全的,如果需要在多线程环境中使用,需要外部同步。

概述

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)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// 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)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// 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()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// 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()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// 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;
            }
        }
    }

  

posted on   anpeiyong  阅读(15)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示