LinkedHashMap 初探

代码中遇到LRUCache的数据结构,仔细研究一下底层就是一个LinkedHashMap的基础数据结构,于是决定简单的总结一下LinkedHashMap的源码。

public class LinkedHashMap<K,V>
    extends HashMap<K,V>
    implements Map<K,V>
可以看出LinkedHashMap是继承于HashMap类的同时也是对Map结构的一种实现。那LinkedHashMap内部实现结构与HashMap相比有什么不同呢,用一句话来概括一下:

LinkedHashMap在HashMap原有的Hash表结构基础上将HashMap中的所有节点维护为一个双向循环链表的结构,从而似的LinkedHashMap能够支持按照key-value的插入顺序(Insertion-order)或者访问顺序(Access-order)去遍历访问。

1.对HashMap的Entry类重写

LinkedHashMap中对HashMap中的Entry类进行了重写,这是实现双向循环链表的基本数据结构。

/**
     * LinkedHashMap entry.
     */
    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) {                  //重写父类的recordAccess函数,对双向链表进行维护
            LinkedHashMap<K,V> lm = (LinkedHashMap<K,V>)m;
            if (lm.accessOrder) {
                lm.modCount++;
                remove();
                addBefore(lm.header);                        //这里很重要的一点:每次将最近访问的节点移动到双向链表的虚拟header节点之前
            }
        }

        void recordRemoval(HashMap<K,V> m) {                 //重写父类的recordRemoval函数
            remove();
        }
    }

2.构造函数

这里主要说明参数最多的一个构造函数,其他构造函数无非就是将相关数据域设为默认值。
/**
     * Constructs an empty <tt>LinkedHashMap</tt> instance with the
     * specified initial capacity, load factor and ordering mode.
     *
     * @param  initialCapacity the initial capacity
     * @param  loadFactor      the load factor
     * @param  accessOrder     the ordering mode - <tt>true</tt> for
     *         access-order, <tt>false</tt> for insertion-order
     * @throws IllegalArgumentException if the initial capacity is negative
     *         or the load factor is nonpositive
     */
    public LinkedHashMap(int initialCapacity,
			 float loadFactor,
                         boolean accessOrder) {
        super(initialCapacity, loadFactor);
        this.accessOrder = accessOrder;       //用于判断当前LinkedHashMap的双向循环链表维护方式,即AccessOrder和InsertionOrder
    }
可以看到这里主要时调用父类的构造函数,同时初始化双向循环链表的维护方式。
    /**
     * Called by superclass constructors and pseudoconstructors (clone,
     * readObject) before any entries are inserted into the map.  Initializes
     * the chain.
     */
    void init() {
        header = new Entry<K,V>(-1, null, null, null);
        header.before = header.after = header;         //初始化双向循环链表的虚拟header节点
    }
这里需要说明的一点时header的声明:
 /**
     * The head of the doubly linked list.
     */
    private transient Entry<K,V> header;
生明中使用了transient关键字,也就是说当一个LinkedHashMap被串行化的时候,header节点是不会被包括在内的,也就是说,header节点并不存储真正有意义的数据域。

3.put操作

LinkedHashMap并没有重写HashMap中的put操作,而是重写了addEntry函数。
/**
     * 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) {
        createEntry(hash, key, value, bucketIndex);

        // Remove eldest entry if instructed, else grow capacity if appropriate
        Entry<K,V> eldest = header.after;
        if (removeEldestEntry(eldest)) {                                   //是否删除最最少访问节点
            removeEntryForKey(eldest.key);        //这里被淘汰的节点始终时header节点的after节点 
        } else {                                 //与HashMap中的处理方式相同,增大capacity重建Hash
            if (size >= threshold)
                resize(2 * table.length);
        }
    }
这里比较有意思的函数是
removeEldestEntry(eldest)
如果你有兴趣看这段注释的话,你会发现他其实视为了给程序员实现Cache提供的一个可扩展接口,也就是说在你自己的Cache类中继承并重写这个函数,就可以实现指定策略的Cache。如将accessOrder设为true,同时按照注释中的示例重写方式,就实现了LRU;如果使用默认accessOrder为false,同时按照注释中的示例重写方式就会保证最早被插入的优先被删除。
/**
     * Returns <tt>true</tt> if this map should remove its eldest entry.
     * This method is invoked by <tt>put</tt> and <tt>putAll</tt> after
     * inserting a new entry into the map.  It provides the implementor
     * with the opportunity to remove the eldest entry each time a new one
     * is added.  This is useful if the map represents a cache: it allows
     * the map to reduce memory consumption by deleting stale entries.
     *
     * <p>Sample use: this override will allow the map to grow up to 100
     * entries and then delete the eldest entry each time a new entry is
     * added, maintaining a steady state of 100 entries.
     * <pre>
     *     private static final int MAX_ENTRIES = 100;
     *
     *     protected boolean removeEldestEntry(Map.Entry eldest) {
     *        return size() > MAX_ENTRIES;
     *     }
     * </pre>
     *
     * <p>This method typically does not modify the map in any way,
     * instead allowing the map to modify itself as directed by its
     * return value.  It <i>is</i> permitted for this method to modify
     * the map directly, but if it does so, it <i>must</i> return
     * <tt>false</tt> (indicating that the map should not attempt any
     * further modification).  The effects of returning <tt>true</tt>
     * after modifying the map from within this method are unspecified.
     *
     * <p>This implementation merely returns <tt>false</tt> (so that this
     * map acts like a normal map - the eldest element is never removed).
     *
     * @param    eldest The least recently inserted entry in the map, or if
     *           this is an access-ordered map, the least recently accessed
     *           entry.  This is the entry that will be removed it this
     *           method returns <tt>true</tt>.  If the map was empty prior
     *           to the <tt>put</tt> or <tt>putAll</tt> invocation resulting
     *           in this invocation, this will be the entry that was just
     *           inserted; in other words, if the map contains a single
     *           entry, the eldest entry is also the newest.
     * @return   <tt>true</tt> if the eldest entry should be removed
     *           from the map; <tt>false</tt> if it should be retained.
     */
    protected boolean removeEldestEntry(Map.Entry<K,V> eldest) {
        return false;
    }
通过createEntry实现了新的key-value插入,
    /**
     * This override differs from addEntry in that it doesn't resize the
     * table or remove the eldest entry.
     */
    void createEntry(int hash, K key, V value, int bucketIndex) {
        HashMap.Entry<K,V> old = table[bucketIndex];
	Entry<K,V> e = new Entry<K,V>(hash, key, value, old);     
        table[bucketIndex] = e;      //三步保证插入始终在bucketIndex的邻接链表头
        e.addBefore(header);         //维护双向循环链表,这里保证新插入的节点始终在header的前面
        size++;
    }
到这里就是先了put的全部功能

4.get操作

LinkedHashMap重写了HashMap的get操作,没什么好说,按照key查询的时间复杂度没变
    /**
     * Returns the value to which the specified key is mapped,
     * or {@code null} if this map contains no mapping for the key.
     *
     * <p>More formally, if this map contains a mapping from a key
     * {@code k} to a value {@code v} such that {@code (key==null ? k==null :
     * key.equals(k))}, then this method returns {@code v}; otherwise
     * it returns {@code null}.  (There can be at most one such mapping.)
     *
     * <p>A return value of {@code null} does not <i>necessarily</i>
     * indicate that the map contains no mapping for the key; it's also
     * possible that the map explicitly maps the key to {@code null}.
     * The {@link #containsKey containsKey} operation may be used to
     * distinguish these two cases.
     */
    public V get(Object key) {
        Entry<K,V> e = (Entry<K,V>)getEntry(key);  //调用父类的getKey
        if (e == null)
            return null;
        e.recordAccess(this);                      //调用重写过得recordAccess在适当情况下维护双向链表
        return e.value;
    }

5.被优化的一些函数

containsValue通过直接遍历双向链表,对原有的HashMap进行了优化
/**
     * Returns <tt>true</tt> if this map maps one or more keys to the
     * specified value.
     *
     * @param value value whose presence in this map is to be tested
     * @return <tt>true</tt> if this map maps one or more keys to the
     *         specified value
     */
    public boolean containsValue(Object value) {
        // Overridden to take advantage of faster iterator
        if (value==null) {
            for (Entry e = header.after; e != header; e = e.after)
                if (e.value==null)
                    return true;
        } else {
            for (Entry e = header.after; e != header; e = e.after)
                if (value.equals(e.value))
                    return true;
        }
        return false;
    }

6.小结

LinkedHashMap通过对Map中的Entry重写定义,在原有的Hash表数据结构基础上维护了一个双向循环链表,从而记录了Map中的数据插入/访问顺序,同时对Iterator进行了重写。
LinkedHashMap同样没有任何线程同步操作,所以不支持多线程并发访问。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值