LinkedHashMap

来源:互联网 发布:素质测评软件 编辑:程序博客网 时间:2024/06/08 05:54

1.类声明:

//使用Hash表和双向列表存放数据public class LinkedHashMap<K,V>    extends HashMap<K,V>    implements Map<K,V>{}

2.变量:

//继承HashMap的节点同时增加前后节点属性 static class Entry<K,V> extends HashMap.Node<K,V> {        Entry<K,V> before, after;        Entry(int hash, K key, V value, Node<K,V> next) {            super(hash, key, value, next);        }    }    //双向列表头结点     transient LinkedHashMap.Entry<K,V> head;     //双向列表尾节点      transient LinkedHashMap.Entry<K,V> tail;      //访问顺序,按插入顺序或是访问顺序(get方法调用顺序),如果按访问顺序,则调用get,put后将节点移到双向列表尾端       final boolean accessOrder;

3.方法:

//把节点移动到双向列表最尾部,LinkedHashMap相比较HashMap的最大区别就是内部维持了一个双向列表,put和get方法中调用了此方法来保证accessOrder为true时列表中节点为LRU顺序,最近最少使用的放在链表头    void afterNodeRemoval(Node<K,V> e) { // unlink        LinkedHashMap.Entry<K,V> p =            (LinkedHashMap.Entry<K,V>)e, b = p.before, a = p.after;        p.before = p.after = null;        if (b == null)            head = a;        else            b.after = a;        if (a == null)            tail = b;        else            a.before = b;    }

参考文章:http://blog.csdn.net/u012403290/article/details/70143443?locationNum=14&fps=1

原创粉丝点击