Understanding Java HashMap Optimizations in JDK 1.8
This article explains the fundamental principles of Java's HashMap, covering hash basics, time‑complexity of operations, the role of load factor and initial capacity, the resize algorithm without full rehashing, and the treeification of long buckets, illustrated with JDK 1.8 source code examples.
HashMap is a core data structure for Java and Android developers, and JDK 1.8 introduces several optimizations that are worth understanding.
Basic hash principles : a hash table maps keys to buckets using a hash code; collisions are resolved with linked lists that may be transformed into red‑black trees when a bucket becomes too large.
Time complexity : lookup is O(1) in the ideal case, O(n) in the worst case, and O(log n) when a bucket has been treeified. The load factor (default 0.75) balances memory usage and lookup speed; smaller values reduce collisions but increase memory consumption, while larger values do the opposite.
When resize occurs : when the number of entries exceeds threshold = capacity × loadFactor, the table doubles in size. JDK 1.8’s resize avoids full rehashing by keeping each entry either at its original index or at index + oldCapacity, determined by (e.hash & oldCap) == 0.
Key methods :
final V putVal(int hash, K key, V value, boolean onlyIfAbsent, boolean evict) { ... }This method handles insertion, collision detection, updating existing keys, and delegating to treeifyBin when a bucket reaches TREEIFY_THRESHOLD (8 entries). final Node<K,V>[] resize() { ... } The resize routine doubles the capacity, splits existing trees, and redistributes entries between the low and high parts of the new table without recomputing full hashes.
Other important members :
static final int DEFAULT_INITIAL_CAPACITY = 1 << 4; // 16 static final float DEFAULT_LOAD_FACTOR = 0.75f; static final int TREEIFY_THRESHOLD = 8;Understanding these parameters helps developers set appropriate initial capacities to minimize rehashing and choose suitable load factors for their workloads.
Because HashMap is not synchronized, concurrent modifications require external synchronization, e.g., wrapping the map with Collections.synchronizedMap(new HashMap<>(...)) or using concurrent alternatives.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Beike Product & Technology
As Beike's official product and technology account, we are committed to building a platform for sharing Beike's product and technology insights, targeting internet/O2O developers and product professionals. We share high-quality original articles, tech salon events, and recruitment information weekly. Welcome to follow us.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
