From f1f03bdf6db5a031caf84f9f6253afcb5f4f8245 Mon Sep 17 00:00:00 2001 From: javahongxi Date: Tue, 13 Aug 2019 20:13:51 +0800 Subject: [PATCH] collections --- .../hongxi/java/util/collections/HashMap.java | 123 +++++++++--------- 1 file changed, 60 insertions(+), 63 deletions(-) diff --git a/whatsmars-common/src/test/java/org/hongxi/java/util/collections/HashMap.java b/whatsmars-common/src/test/java/org/hongxi/java/util/collections/HashMap.java index c736682b..52f2b590 100644 --- a/whatsmars-common/src/test/java/org/hongxi/java/util/collections/HashMap.java +++ b/whatsmars-common/src/test/java/org/hongxi/java/util/collections/HashMap.java @@ -4,6 +4,7 @@ import java.util.Objects; /** * @author shenhongxi 2019/8/13 + * @see java.util.Hashtable */ public class HashMap { @@ -20,15 +21,11 @@ public class HashMap { /** * The table is rehashed when its size exceeds this threshold. (The * value of this field is (int)(capacity * loadFactor).) - * - * @serial */ private int threshold; /** * The load factor for the hashtable. - * - * @serial */ private float loadFactor; @@ -193,64 +190,6 @@ public class HashMap { */ private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8; - /** - * Increases the capacity of and internally reorganizes this - * hashtable, in order to accommodate and access its entries more - * efficiently. This method is called automatically when the - * number of keys in the hashtable exceeds this hashtable's capacity - * and load factor. - */ - @SuppressWarnings("unchecked") - protected void rehash() { - int oldCapacity = table.length; - Entry[] oldMap = table; - - // overflow-conscious code - int newCapacity = (oldCapacity << 1) + 1; - if (newCapacity - MAX_ARRAY_SIZE > 0) { - if (oldCapacity == MAX_ARRAY_SIZE) - // Keep running with MAX_ARRAY_SIZE buckets - return; - newCapacity = MAX_ARRAY_SIZE; - } - Entry[] newMap = new Entry[newCapacity]; - - modCount++; - threshold = (int)Math.min(newCapacity * loadFactor, MAX_ARRAY_SIZE + 1); - table = newMap; - - for (int i = oldCapacity ; i-- > 0 ;) { - for (Entry old = (Entry)oldMap[i]; old != null ; ) { - Entry e = old; - old = old.next; - - int index = (e.hash & 0x7FFFFFFF) % newCapacity; - e.next = (Entry)newMap[index]; - newMap[index] = e; - } - } - } - - private void addEntry(int hash, K key, V value, int index) { - modCount++; - - Entry tab[] = table; - if (count >= threshold) { - // Rehash the table if the threshold is exceeded - rehash(); - - tab = table; - hash = key.hashCode(); - index = (hash & 0x7FFFFFFF) % tab.length; - } - - // Creates the new entry. - @SuppressWarnings("unchecked") - Entry e = (Entry) tab[index]; - tab[index] = new Entry<>(hash, key, value, e); - count++; - } - /** * Maps the specified key to the specified * value in this hashtable. Neither the key nor the @@ -292,6 +231,64 @@ public class HashMap { return null; } + private void addEntry(int hash, K key, V value, int index) { + modCount++; + + Entry tab[] = table; + if (count >= threshold) { + // Rehash the table if the threshold is exceeded + rehash(); + + tab = table; + hash = key.hashCode(); + index = (hash & 0x7FFFFFFF) % tab.length; + } + + // Creates the new entry. + @SuppressWarnings("unchecked") + Entry e = (Entry) tab[index]; + tab[index] = new Entry<>(hash, key, value, e); + count++; + } + + /** + * Increases the capacity of and internally reorganizes this + * hashtable, in order to accommodate and access its entries more + * efficiently. This method is called automatically when the + * number of keys in the hashtable exceeds this hashtable's capacity + * and load factor. + */ + @SuppressWarnings("unchecked") + protected void rehash() { + int oldCapacity = table.length; + Entry[] oldMap = table; + + // overflow-conscious code + int newCapacity = (oldCapacity << 1) + 1; + if (newCapacity - MAX_ARRAY_SIZE > 0) { + if (oldCapacity == MAX_ARRAY_SIZE) + // Keep running with MAX_ARRAY_SIZE buckets + return; + newCapacity = MAX_ARRAY_SIZE; + } + Entry[] newMap = new Entry[newCapacity]; + + modCount++; + threshold = (int)Math.min(newCapacity * loadFactor, MAX_ARRAY_SIZE + 1); + table = newMap; + + for (int i = oldCapacity ; i-- > 0 ;) { + for (Entry old = (Entry)oldMap[i]; old != null ; ) { + Entry e = old; + old = old.next; + + int index = (e.hash & 0x7FFFFFFF) % newCapacity; + e.next = (Entry)newMap[index]; + newMap[index] = e; + } + } + } + /** * Removes the key (and its corresponding value) from this * hashtable. This method does nothing if the key is not in the hashtable. @@ -330,7 +327,7 @@ public class HashMap { public void clear() { Entry tab[] = table; modCount++; - for (int index = tab.length; --index >= 0; ) + for (int index = tab.length; --index >= 0;) tab[index] = null; count = 0; } -- GitLab