Use of recordAccess(this) in Entry class

2019-08-12 16:53发布

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条回答
甜甜的少女心
2楼-- · 2019-08-12 17:02

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.

查看更多
登录 后发表回答