【Java】LinkedHashMap

Java LinkedHashMap

标签(空格分隔):Java source-code


总结

1.LinkedHashMap基于HashMap,实现了按插入顺序、LRU,实现方式主要是继承了HashMap的Entry类,构建双向链表
2.accessOrder 为true时候,链表排序算法为LRU ,初始化中设置该参数
3.对同一key的覆盖处理,插入排序时候不会改变该entry在双向链表中的位置,lru中会被提到header.before位置
4.LinkedHashMap的遍历为遍历整个双向链表,而HashMap则需要先遍历table,之后遍历各个子链表,需要全局遍历时候LinkehhahMap更高效。因此在所有需要全局遍历的方法中LinkedHashMap都做了相应的重写。
5. LRU应用中可重写removeEldestEntry函数,为可重写的方法 ,作为是否删除最不常用元素的判断条件

LinkedHashMap数据结构

LinkedHashMap在HashMap的基础上实现了两种排序(插入顺序,最近使用顺序LRU),在支持快速存取元素的同时,支持了按插入活着LRU算法的迭代访问。
实现方式为hashmap基础上,增加了双向链表。
img

LinkedHashMap的entry继承了hashmap的内部类entry,在原有基础上增加了after和before指针,保存了原有数据结构的基础上维护了一个双向链表。把双向链表单独抽出来看,如下:
img

双向链表,header为表头,不存实际数据,新加入的元素放在heade.before位置,迭代器从header.after开始遍历,实现按插入顺序遍历。

按照插入顺序排序是缺省,构造LinkedHashMap时指定参数可变成LRU,把最不常用的元素放在header.after位置,读取过的元素从表中抽出,再作为最常用元素插入header.before。

实现1_entry

entry继承,添加了after、before指针

private static class Entry<K,V> extends HashMap.Entry<K,V> {
        // These fields comprise the doubly linked list used for iteration.
        Entry<K,V> before, after;

        Entry(int hash, K key, V value, HashMap.Entry<K,V> next) {
            super(hash, key, value, next);
        }

        /**
         * Removes this entry from the linked list.
         */
        private void remove() {
            before.after = after;
            after.before = before;
        }

        /**
         * Inserts this entry before the specified existing entry in the list.
         */
        private void addBefore(Entry<K,V> existingEntry) {
            after  = existingEntry;
            before = existingEntry.before;
            before.after = this;
            after.before = this;
        }

        /**
         * This method is invoked by the superclass whenever the value
         * of a pre-existing entry is read by Map.get or modified by Map.set.
         * If the enclosing Map is access-ordered, it moves the entry
         * to the end of the list; otherwise, it does nothing.
         */
        void recordAccess(HashMap<K,V> m) {
            LinkedHashMap<K,V> lm = (LinkedHashMap<K,V>)m;
            if (lm.accessOrder) {
                lm.modCount++;
                remove();
                addBefore(lm.header);
            }
        }

        void recordRemoval(HashMap<K,V> m) {
            remove();
        }
    }

增加的双向链表中移除entry,特定位置插入entry。
recordaccess方法只在accessorder为true时有用,即是在entry被读取时候是否对其在双向链表中位置进行挑战。LRU中被读取的entry被插入刀header.before位置。

实现2_双向链表构建

改变双向链表的时机在

1.新加入entry,
2.取出entry,对于lru有影响

addEntry
注意加入时候有一个removeEldestEntry函数,为可重写的方法 ,作为是否删除最不常用元素的判断条件

/**
     * This override alters behavior of superclass put method. It causes newly
     * allocated entry to get inserted at the end of the linked list and
     * removes the eldest entry if appropriate.
     */
    void addEntry(int hash, K key, V value, int bucketIndex) {
        super.addEntry(hash, key, value, bucketIndex);

        // Remove eldest entry if instructed
        Entry<K,V> eldest = header.after;
        if (removeEldestEntry(eldest)) {
            removeEntryForKey(eldest.key);
        }
    }

getEntry

public V get(Object key) {
        Entry<K,V> e = (Entry<K,V>)getEntry(key);
        if (e == null)
            return null;
        e.recordAccess(this);
        return e.value;
    }

链接

源码分析

posted on 2014-10-02 15:12  yyjaaa  阅读(425)  评论(0编辑  收藏  举报