首页 > 其他分享 >Map---IdentityHashMap

Map---IdentityHashMap

时间:2023-11-15 15:04:54浏览次数:41  
标签:Map java class --- key null public 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)

// 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;
            }
        }
    }

  

标签:Map,java,class,---,key,null,public,IdentityHashMap
From: https://www.cnblogs.com/anpeiyong/p/17833843.html

相关文章

  • Vue 2.x脱坑记-查漏补缺
    Q:组件的通讯有哪几种啊!基本最常用的是这三种;父传子: props子传父: emit兄弟通讯:eventbus:就是找一个中间组件来作为信息传递中介vuex:信息树传送门:基本通讯VuexQ:为什么我的 npm 或者 yarn 安装依赖会生成 lock文件,有什么用!lock文件的作用......
  • 如何在mapbox中将标注添加到面
    consttestGeoJOSN=()=>{//加载GeoJSON数据map.addSource("geojson",{type:"geojson",data:china,generateId:true,});map.addLayer({id:"china",type:"fill",......
  • leecode(sql)--简单
    2023.11.15175.组合两个表https://leetcode.cn/problems/combine-two-tables/description/代码:selectp.firstName,p.lastName,a.city,a.statefromPersonpleftjoinAddressaona.personId=p.personId181.......
  • 五分钟k8s实战-Istio 网关
    在上一期k8s-服务网格实战-配置Mesh中讲解了如何配置集群内的Mesh请求,Istio同样也可以处理集群外部流量,也就是我们常见的网关。其实和之前讲到的k8s入门到实战-使用IngressIngress作用类似,都是将内部服务暴露出去的方法。只是使用Istio-gateway会更加灵活。这里有......
  • RK3568-dmesg
    [0.000000]BootingLinuxonphysicalCPU0x0000000000[0x412fd050][0.000000]Linuxversion4.19.161AIMY-RK3568(jenkins@jenkins-seafile-gitlab-60)(gccversion6.3.120170404(LinaroGCC6.3-2017.05),GNUld(Linaro_Binutils-2017.05)2.27.0.20161......
  • RK3328-dmesg
    [0.000000]BootingLinuxonphysicalCPU0x0[0.000000]Initializingcgroupsubsyscpuset[0.000000]Initializingcgroupsubsyscpu[0.000000]Initializingcgroupsubsyscpuacct[0.000000]Linuxversion4.4.194(jenkins@jenkins-seafile......
  • Vue双向数据绑定原理-上
    Vue响应式的原理(数据改变界面就会改变)是什么?时时监听数据变化,一旦数据发生变化就更新界面,这就是Vue响应式的原理。Vue是如何实现时时监听数据变化的通过原生JS的defineProperty方法,通过get和set方法来监听数据的变化。defineProperty方法的特点可以直接在一个对象上定义一......
  • TienChin-课程管理-课程导出
    更改Course.java:/***课程ID*/@TableId(value="course_id",type=IdType.AUTO)@NotNull(message="{course.id.notnull}")@Excel(name="课程编号")privateIntegercourseId;/***课程类型1.舞蹈类2.游泳类3.拳击类*/@NotNull(message=......
  • TienChin-课程管理-课程搜索
    后端新建CourseVO.java:/***CourseVO类是一个课程的值对象,用于存储课程的相关信息。*它包含了课程的名称、类型、适用对象、最低价格和最高价格等属性。*/publicclassCourseVO{privateStringname;//课程名称privateStringtype;//课程类型privat......
  • TienChin-课程管理-删除课程
    CourseController.java@PreAuthorize("hasPermission('tienchin:course:remove')")@Log(title="课程管理",businessType=BusinessType.DELETE)@DeleteMapping("/{courseIds}")AjaxResultremove(@PathVariableObject[]courseId......