zoukankan      html  css  js  c++  java
  • LinkedHashMap随笔

    LinkedHashMap继承了HashMap, 采用了HashMap的散列结构 因此其随机查询速度很快. 同时由于其修改HashMap.Entry, 添加 before/after Entry<K,V>两个元素用于保存最后添加的元素 以保持顺序结构.

    private transient Entry<K,V> header;
    
    public void clear() {
            super.clear();
            header.before = header.after = header;
        }
    
    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();
            }
        }
    
    /**
         * 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;
        }
    
        /**
         * Transfers all entries to new table array.  This method is called
         * by superclass resize.  It is overridden for performance, as it is
         * faster to iterate using our linked list.
         */
        void transfer(HashMap.Entry[] newTable) {
            int newCapacity = newTable.length;
            for (Entry<K,V> e = header.after; e != header; e = e.after) {
                int index = indexFor(e.hash, newCapacity);
                e.next = newTable[index];
                newTable[index] = e;
            }
        }
  • 相关阅读:
    总结Cnblogs支持的常用Markdown语法
    Python导出Excel为Lua/Json/Xml实例教程(一):初识Python
    我的博客今天开通了,请多指教!
    技术分析:Femtocell家庭基站通信截获、伪造任意短信
    数据库防火墙如何防范SQL注入行为
    Pjax.js防刷新技术
    【写给新人】做开发几年的个人经历
    WebRTC之PeerConnection的建立过程
    基于Licode demo的屏幕共享功能的实现
    基于WebRTC的MCU开源项目Licode的环境搭建
  • 原文地址:https://www.cnblogs.com/davidwang/p/2823740.html
Copyright © 2011-2022 走看看