标签:支持 put cto method other nothing ash when 实现
Java7/8中HashMap(和 ConcurrentHashMap)的相关基本操作源码介绍,这里可以直接参考【Java7/8 中的 HashMap 和 ConcurrentHashMap 全解析】,介绍得还是挺详细的,就不班门弄斧了。
关于Java7的HashMap,这里主要提几点,具体的分析还是请参考上面给的链接,
Java8的HashMap,最大的改变,是使用了数组+链表+红黑树。当链表中的元素达到了 8 个时,会将链表转换为红黑树,在这些位置进行查找的时候可以由原来的耗时O(N),降低时间复杂度为 O(logN)。
关于红黑树和ConcurrentHashMap,有待后续的进一步研究。
另外还要提到的是,关于HashMap中的一些扩展点。
1、init()方法:在LinkedHashMap,该方法用于进行虚结点的初始化。
/** * Constructs an empty <tt>HashMap</tt> with the specified initial * capacity and load factor. * * @param initialCapacity the initial capacity * @param loadFactor the load factor * @throws IllegalArgumentException if the initial capacity is negative * or the load factor is nonpositive * HashMap中的方法 */ public HashMap(int initialCapacity, float loadFactor) { if (initialCapacity < 0) throw new IllegalArgumentException("Illegal initial capacity: " + initialCapacity); if (initialCapacity > MAXIMUM_CAPACITY) initialCapacity = MAXIMUM_CAPACITY; if (loadFactor <= 0 || Float.isNaN(loadFactor)) throw new IllegalArgumentException("Illegal load factor: " + loadFactor); this.loadFactor = loadFactor; threshold = initialCapacity; init(); } /** * Called by superclass constructors and pseudoconstructors (clone, * readObject) before any entries are inserted into the map. Initializes * the chain. * LinkHashMap中重写之后的方法 */ @Override void init() { header = new Entry<>(-1, null, null, null); header.before = header.after = header; }
2、Entry的recordAccess方法:HashMap中是空实现,从名字可以看出“记录访问”,在LinkedHashMap中进行了重写,用于根据插入或者访问元素的顺序来调整链表元素。
/** * This method is invoked whenever the value in an entry is * overwritten by an invocation of put(k,v) for a key k that‘s already * in the HashMap. * HashMap中Entry的方法 */ void recordAccess(HashMap<K,V> m) { } // 以下的是LinkedHashMap中Entry的方法 /** * 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); } }
另外还有几个点,如get(Object key)、addEntry(int hash, K key, V value, int bucketIndex)、createEntry(int hash, K key, V value, int bucketIndex)等,在LinkedHashMap中都进行了重写,用于支持根据插入或者访问元素的顺序来调整链表元素,LinkedHashMap相关的控制变量源码如下,这里大致说明下操作的顺序:
相关具体说明后续在介绍LinkedHashMap时会进一步细讲。
/** * The iteration ordering method for this linked hash map: <tt>true</tt> * for access-order, <tt>false</tt> for insertion-order. * * @serial */ private final boolean accessOrder; //true表示按照访问顺序,默认false表示按照插入顺序,调整特定元素至尾部
标签:支持 put cto method other nothing ash when 实现
原文地址:https://www.cnblogs.com/wpbxin/p/8684747.html