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;
            }
        }
  • 相关阅读:
    计算机视觉资源合集
    EMZ-搭建DL模型的最简便的方式 | 附项目地址
    在边缘设备上拟合大型神经网络的方法总结
    归一化方法总结 | 又名“BN和它的后浪们“
    名词解释 | 论文中的Ablation study
    知识蒸馏的简要概述
    如何看待计算机视觉未来的走向(二)从产品的角度聊一聊
    STL源码分析--functional
    STL源码分析--algorithm
    STL源码分析--bitset
  • 原文地址:https://www.cnblogs.com/davidwang/p/2827734.html
Copyright © 2011-2022 走看看