java源码之浅谈LinkedHashMap

来源:互联网 发布:mac tree 编辑:程序博客网 时间:2024/05/17 22:49

LinkedHashMap是继承HashMap的。

public class LinkedHashMap<K,V> extends HashMap<K,V> implements Map<K,V>

它Node节点的值还是存在hash表中的,但是它还保存了一份在一个链表中。根据插入的顺序来构建链表的。

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

newNode方法

    Node<K,V> newNode(int hash, K key, V value, Node<K,V> e) {        LinkedHashMap.Entry<K,V> p =            new LinkedHashMap.Entry<K,V>(hash, key, value, e);        linkNodeLast(p);        return p;    }        private void linkNodeLast(LinkedHashMap.Entry<K,V> p) {        LinkedHashMap.Entry<K,V> last = tail;        tail = p;        if (last == null)            head = p;        else {            p.before = last;            last.after = p;        }    }

插入元素的时候会调用linkNodeLast,将插入的元素放到链表的最后。

重写HashMap其中的这3个空方法了,当我们查看HashMap的源码的时候,在put,remove方法的时候,都把这些方法预先放进去的,利用了模板设计模式。

    // Callbacks to allow LinkedHashMap post-actions    void afterNodeAccess(Node<K,V> p) { }    void afterNodeInsertion(boolean evict) { }    void afterNodeRemoval(Node<K,V> p) { }

afterNodeAccess方法
LinkedHashMap可以启用LRU模式,启用后,每次访问过的元素都会被放到tail位置

    void afterNodeAccess(Node<K,V> e) { // move node to last        LinkedHashMap.Entry<K,V> last;        //当accessOrder,且tail不为e        if (accessOrder && (last = tail) != e) {         //用a和b分别记录该节点前面和后面的节点            LinkedHashMap.Entry<K,V> p =                (LinkedHashMap.Entry<K,V>)e, b = p.before, a = p.after;         //释放当前节点与后节点的关系             p.after = null;           //如果当前节点的前节点是空            if (b == null)            //那么头节点就设置为a                head = a;            else                b.after = a;            //如果a节点不为空            if (a != null)             //a的后节点指向b                a.before = b;            else             //如果a为空,那么b就是尾节点                last = b;             //如果尾节点为空            if (last == null)             //那么p为头节点                head = p;            else {            //否则就把p放到双向链表最尾处                p.before = last;                last.after = p;            }            //设置尾节点为P            tail = p;            //操作次数加1            ++modCount;        }    }

就是把当前节点e移至链表的尾部。因为使用的是双向链表。并且只有当accessOrder设置为true时,才会执行这个操作。在HashMap的putVal方法中,就调用了这个方法。

afterNodeInsertion方法

    void afterNodeInsertion(boolean evict) { // possibly remove eldest        LinkedHashMap.Entry<K,V> first;        if (evict && (first = head) != null && removeEldestEntry(first)) {            K key = first.key;            removeNode(hash(key), key, null, false, true);        }    }     protected boolean removeEldestEntry(Map.Entry<K,V> eldest) {        return false;    }

默认情况下,这个是不会执行到的,除非你重写removeEldestEntry方法。

afterNodeRemoval方法

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

这个方法是当HashMap删除一个键值对时调用的,它会把在HashMap中删除的那个键值对一并从链表中删除,保证了哈希表和链表的一致性。
get方法

    public V get(Object key) {        Node<K,V> e;        if ((e = getNode(hash(key), key)) == null)            return null;        if (accessOrder)            afterNodeAccess(e);        return e.value;    }

调用了HashMap中的getNode方法来找,如accessOrder为ture还要调用afterNodeAccess来维护链表。

用iterator来遍历的时候,得到的元素会是先插入的(accessOrder 为false的情况下)。

原创粉丝点击