Concept
HashMap
Average lookup and update are O(1) when hashes spread well. Heavy collisions degrade toward O(n) — interview material covers capacity and load factor in depth.
Where it fits
Packages: Core Java (pkg1core)
Learn
Existing chapters for this concept.
- Collections
Average lookup and update are O(1) when hashes spread well. Heavy collisions degrade toward O(n) — interview material covers capacity and load factor in depth.
Open lesson →
See it in code
Existing Java examples.
- core 29 Hash Map Demo
- Keys are unique; put with an existing key replaces the value. - Lookup uses hashCode (bucket) then equals (match within the bucket). - Mutating a key after…
Open example → - core 19 Collections Demo
- List: ordered, indexed, allows duplicates (ArrayList / LinkedList). - Set: no duplicates (HashSet=unordered, LinkedHashSet=insertion, TreeSet=sorted). -…
Open example →
Practice
Existing LeetCode material in JavaForge.
- Two Sum
One-pass hash map stores value->index; check complement each step. Time O(n), Space O(n)
Open practice → - LRU Cache
HashMap + doubly linked list for O(1) get/put eviction. Time O(1) per op, Space O(capacity)
Open practice → - Isomorphic Strings
Two hash maps enforce one-to-one char mapping. Time O(n), Space O(1)
Open practice → - Contains Duplicate II
Hash map stores last index; check distance <= k. Time O(n), Space O(n)
Open practice → - Subarray Sum Equals K
Prefix sum hash map counts subarrays with needed prefix. Time O(n), Space O(n)
Open practice → - Two Sum
Given an array and a target, return indices of the two numbers that add up to target. one-pass hash map. For each x, check if (target - x) was seen. Time…
Open practice → - Unique Number of Occurrences
Count frequencies; set size equals max frequency count. Time O(n), Space O(n)
Open practice → - Determine if Two Strings Have Equal Character Frequency
Same length and same sorted char frequency arrays. Time O(n), Space O(1)
Open practice → - Max Number of K-Sum Pairs
Hash map counts complements for k-sum pairs. Time O(n), Space O(n)
Open practice → - Find the Difference of Two Arrays
Sets for unique elements in each direction. Time O(n+m), Space O(n+m)
Open practice → - Equal Row and Column Pairs
Hash row signatures; count matching columns. Time O(n^2), Space O(n^2)
Open practice →
Prepare
Existing interview questions.
- How does HashMap work internally?
Array of buckets; index from hash; chaining; treeify on heavy collisions.
Open question → - HashMap vs Hashtable vs ConcurrentHashMap?
HashMap (not synced, allows null), Hashtable (legacy, fully synced), ConcurrentHashMap (scalable concurrency).
Open question → - How does ConcurrentHashMap achieve concurrency?
Bucket-level CAS + synchronized bins; no global lock.
Open question → - What is the load factor and capacity?
Capacity = bucket count; load factor = fill threshold (0.75).
Open question → - Why must map keys be immutable / have stable hashCode?
Changing a key's hash after insertion makes it unfindable.
Open question →