record< K,V>中使用recordAccess(this)类 [英] Use of recordAccess(this) in Entry<K, V> class

查看:466
本文介绍了record< K,V>中使用recordAccess(this)类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  / ** 
*在HashMap类中查看时,我遇到了这个方法:只要在HashMap中已经存在
*的键k对put(k,v)的调用覆盖了条目中的值
*,就会调用此方法。
* /
void recordAccess(HashMap< K,V> m){
}

实际上,这个方法是在 Entry的内部类中定义的



我无法评论这个评论。这种方法做什么?



PS:我也可以看到这个方法在HashMap的putForNullKey()方法中被调用。 (Entry< K,V> e = table [0]; e!= null; e = e.next){
if($($))$ private $ V putForNullKey(V value){
e.key == null){
V oldValue = e.value;
e.value = value;
e.recordAccess(this); //调用
返回oldValue;
}
}
modCount ++;
addEntry(0,null,value,0);
返回null;
}

更新:我更新了第一个代码片段。

解决方案

一个LinkedHashMap可以有两个顺序:插入顺序或访问顺序。如果使用访问顺序,则此方法可确保访问的条目移至列表的开头。


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.

解决方案

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.

这篇关于record&lt; K,V&gt;中使用recordAccess(this)类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆