Use of recordAccess(this) in Entry<K, V> class

 ̄綄美尐妖づ 提交于 2019-12-11 06:06:16

问题


While having a look inside HashMap class, I came across this method :-

    /**
     * This method is invoked whenever the value in an entry is
     * overwritten by an invocation of put(k,v) for a key k that's already
     * in the HashMap.
     */
    void recordAccess(HashMap<K,V> m) {
    }

Actually, this method is defined inside the inner class of Entry<K, V>

I'm not able to make out of that comment. What does this method do ?

PS : I can also see this method being called inside's HashMap's putForNullKey() method

 private V putForNullKey(V value) {
        for (Entry<K,V> e = table[0]; e != null; e = e.next) {
            if (e.key == null) {
                V oldValue = e.value;
                e.value = value;
                e.recordAccess(this); // call
                return oldValue;
            }
        }
        modCount++;
        addEntry(0, null, value, 0);
        return null;
    }

UPDATE : I've updated the first code snippet.


回答1:


A LinkedHashMap can have two orders: insertion order, or access order. If access order is used, this method makes sure that the accessed entry is moved to the beginning of the list.



来源:https://stackoverflow.com/questions/17703668/use-of-recordaccessthis-in-entryk-v-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!