LinkedHashMap双向链表

双链回环循环链表,名字相当唬人。首尾相接才算循环,但1.8这个没发现有首尾相接,LinkedList也没有

LinkedHashMap,有两种排序,一种是顺序存储排序,像队列,先放入的元素在队头,后放入的元素在队尾,移除操作在队头进行。另一种是访问排序,被访问的元素放在队尾,没被访问的放队头,put get这些操作都算是访问

1.顺序存储排序

LinkedHashMap继承了HashMap,比其多了两个属性head 和tail,加了一种链表的数据

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

    static class Entry<K,V> extends HashMap.Node<K,V> { //继承了HashMap内部节点类,加了两个属性before上个节点,after和下个节点
        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;//尾节点

    /**
     * The iteration ordering method for this linked hash map: <tt>true</tt>
     * for access-order, <tt>false</tt> for insertion-order.
     *
     * @serial
     */
    final boolean accessOrder; //如果是false,则用存储排序,true用访问排序,LinkedHashMap的构造方法默认都是false


    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;
        }
    }
    
    //重写的HashMap的newNode方法,除了hashmap的方法外,加了把节点放在尾端
    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;
    }

LinkedHashMap其实没有重写HashMap的put方法,它并有放弃HashMap的散列链表,只是在这个基础上加上了自己的链表。 来看HashMap的源码,put方法调用了newNode方法,LinkedHashMap只需要重写此方法就可以在put时把元素也放在自己的链表中,新节点变成尾节点

    final V putVal(int hash, K key, V value, boolean onlyIfAbsent,
                   boolean evict) {
        Node<K,V>[] tab; Node<K,V> p; int n, i;
        if ((tab = table) == null || (n = tab.length) == 0)
            n = (tab = resize()).length;
        if ((p = tab[i = (n - 1) & hash]) == null)
            tab[i] = newNode(hash, key, value, null);
        else {
            Node<K,V> e; K k;
            if (p.hash == hash &&
                ((k = p.key) == key || (key != null && key.equals(k))))
                e = p;
            else if (p instanceof TreeNode)
                e = ((TreeNode<K,V>)p).putTreeVal(this, tab, hash, key, value);
            else {
                for (int binCount = 0; ; ++binCount) {
                    if ((e = p.next) == null) {
                        p.next = newNode(hash, key, value, null);
                        if (binCount >= TREEIFY_THRESHOLD - 1) // -1 for 1st
                            treeifyBin(tab, hash);
                        break;
                    }
                    if (e.hash == hash &&
                        ((k = e.key) == key || (key != null && key.equals(k))))
                        break;
                    p = e;
                }
            }
            if (e != null) { // existing mapping for key
                V oldValue = e.value;
                if (!onlyIfAbsent || oldValue == null)
                    e.value = value;
                afterNodeAccess(e);
                return oldValue;
            }
        }
        ++modCount;
        if (++size > threshold)
            resize();
        afterNodeInsertion(evict);
        return null;
    }   

/* ------------------------------------------------------------ */源码特地注释了 以下是支持LinekdHashMap的
    // LinkedHashMap support


    /*
     * The following package-protected methods are designed to be
     * overridden by LinkedHashMap, but not by any other subclass.
     * Nearly all other internal methods are also package-protected
     * but are declared final, so can be used by LinkedHashMap, view
     * classes, and HashSet.
     */

    // Create a regular (non-tree) node
    Node<K,V> newNode(int hash, K key, V value, Node<K,V> next) {
        return new Node<>(hash, key, value, next);
    }

LinkedHashMap put先调用HashMap的put ,在put中调用了LinkedHashMap的newNode方法(把元素插入双向链表中)

来看看LinkedHashMap的remove方法:先调用HashMap的remove方法,此方法中调用了一个afterNodeRemoval方法,HashMap的afterNodeRemoval是空方法,LinkedHashMap重写了此方法,把元素从双向链表中移除

HashMap:    

final Node<K,V> removeNode(int hash, Object key, Object value,
                               boolean matchValue, boolean movable) {

....省略
if (node != null && (!matchValue || (v = node.value) == value ||
                                 (value != null && value.equals(v)))) {
                if (node instanceof TreeNode)
                    ((TreeNode<K,V>)node).removeTreeNode(this, tab, movable);
                else if (node == p)
                    tab[index] = node.next;
                else
                    p.next = node.next;
                ++modCount;
                --size;
                afterNodeRemoval(node);
                return node;
            }
}

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

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

2.访问排序

想使用访问排序,需要在初始化LinkedHashMap时设置accessOrder为true

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

    void afterNodeAccess(Node<K,V> e) { // move node to last  把被访问的元素 放到队尾
        LinkedHashMap.Entry<K,V> last;
        if (accessOrder && (last = tail) != e) {
            LinkedHashMap.Entry<K,V> p =
                (LinkedHashMap.Entry<K,V>)e, b = p.before, a = p.after;
            p.after = null;
            if (b == null)
                head = a;
            else
                b.after = a;
            if (a != null)
                a.before = b;
            else
                last = b;
            if (last == null)
                head = p;
            else {
                p.before = last;
                last.after = p;
            }
            tail = p;
            ++modCount;
        }
    }


  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值