|
| 1 | +HashMap — Overview, Implementation, and Key Details |
| 2 | + |
| 3 | +Overview |
| 4 | +-------- |
| 5 | +Implementation: |
| 6 | +- HashMap is a hash-table–based implementation of the Map interface. |
| 7 | +- It stores key → value mappings and allows one null key and multiple null values. |
| 8 | + |
| 9 | +Usage: |
| 10 | +- Widely used for fast data retrieval when insertion order or key ordering is not important. |
| 11 | + |
| 12 | +Key Features and Performance |
| 13 | +---------------------------- |
| 14 | +Constant-time operations: |
| 15 | +- Under typical (well-distributed hash) conditions, basic operations such as get() and put() run in O(1) time on average. |
| 16 | + |
| 17 | +Initial Capacity & Load Factor: |
| 18 | +- Initial capacity: number of buckets (default = 16). |
| 19 | +- Load factor: default = 0.75. It balances memory usage and lookup performance. |
| 20 | +- When the number of entries exceeds capacity × load factor, rehashing happens (the table is resized, typically roughly doubled). |
| 21 | + |
| 22 | +Iteration cost: |
| 23 | +- Iterating a HashMap takes time proportional to the number of buckets plus the number of key-value mappings. |
| 24 | +- Choosing an appropriate initial capacity can reduce rehashing and iteration overhead. |
| 25 | + |
| 26 | +Constructors and Initialization |
| 27 | +------------------------------- |
| 28 | +Common constructors: |
| 29 | +- Default constructor: empty map with default capacity (16) and load factor (0.75). |
| 30 | +- Constructor with initial capacity: lets you set starting number of buckets. |
| 31 | +- Constructor with capacity and load factor: tune both parameters. |
| 32 | +- Copy constructor: initialize a new HashMap with mappings from another Map. |
| 33 | + |
| 34 | +Collision Handling and Rehashing |
| 35 | +-------------------------------- |
| 36 | +Collision resolution: |
| 37 | +- A hash of the key determines the bucket (index) for storage. |
| 38 | +- Collisions (multiple keys mapping to the same bucket) are handled internally. |
| 39 | + |
| 40 | +Java 8+ enhanced collision management: |
| 41 | +- Prior to Java 8, each bucket used a singly linked list of entries. |
| 42 | +- Since Java 8, if a single bucket becomes too large (many collisions), the linked list may be converted into a balanced tree (TreeNode) to improve worst-case performance from O(n) to O(log n) for that bucket. |
| 43 | + |
| 44 | +Rehashing: |
| 45 | +- When size > capacity × load factor, the table is resized and entries are redistributed (rehash). |
| 46 | +- Rehashing is relatively expensive because all entries are moved into a new table. |
| 47 | + |
| 48 | +Concurrency and Fail-Fast Behavior |
| 49 | +---------------------------------- |
| 50 | +Thread-safety: |
| 51 | +- HashMap is not synchronized. It is not safe for concurrent modification from multiple threads without external synchronization. |
| 52 | +- For concurrent access, consider ConcurrentHashMap or synchronize externally. |
| 53 | + |
| 54 | +Fail-fast iterators: |
| 55 | +- Iterators returned by HashMap's views (keySet(), entrySet(), values()) are fail-fast. |
| 56 | +- If the map is structurally modified after the iterator is created (other than via the iterator’s own remove), a ConcurrentModificationException is thrown. |
| 57 | + |
| 58 | +Common Methods |
| 59 | +-------------- |
| 60 | +Basic operations: |
| 61 | +- put(K key, V value) — Associates a value with a key. |
| 62 | +- get(Object key) — Retrieves the value for a key. |
| 63 | +- remove(Object key) — Removes the mapping for a key. |
| 64 | +- containsKey(Object key), containsValue(Object value) — Check existence. |
| 65 | +- putAll(Map<? extends K,? extends V> m) — Copy mappings from another map. |
| 66 | +- clear() — Remove all mappings. |
| 67 | + |
| 68 | +Advanced operations (functional-style helpers): |
| 69 | +- compute(), computeIfAbsent(), computeIfPresent(), merge() — Update mappings using lambda-friendly semantics. |
| 70 | +- clone() — Create a shallow copy of the HashMap. |
| 71 | + |
| 72 | +Implementation details (internal structure) |
| 73 | +-------------------------------------------- |
| 74 | +- Backing structure: an array of Node<K,V> references (table[]). |
| 75 | +- Each Node contains: hash, key, value, next (for collision chaining) or tree links (for tree bins). |
| 76 | +- The hash function: HashMap applies a supplemental hash (mix) to key.hashCode() to spread bits and reduce clustering. |
| 77 | +- Index for bucket: computed using (n - 1) & hash when n is the table length (power-of-two length makes this efficient). |
| 78 | + |
| 79 | +Performance considerations |
| 80 | +-------------------------- |
| 81 | +- Good hashCode() implementation on keys reduces collisions and keeps operations near O(1). |
| 82 | +- If many keys share the same bucket (poor hash), performance degrades unless the bucket becomes a tree (Java 8+). |
| 83 | +- Rehashing costs can be reduced by setting an appropriate initial capacity if the approximate number of entries is known. |
| 84 | + |
| 85 | +Null handling |
| 86 | +------------- |
| 87 | +- One null key is allowed (it is stored in bucket 0). |
| 88 | +- Multiple null values are allowed. |
| 89 | + |
| 90 | +Interview points to remember |
| 91 | +---------------------------- |
| 92 | +- Null handling: HashMap allows one null key and multiple null values. |
| 93 | +- Capacity & load factor: default capacity = 16, load factor = 0.75; rehash occurs when size > capacity × load factor. |
| 94 | +- Collision improvements: Java 8 converts long collision chains into balanced trees for better worst-case performance. |
| 95 | +- Thread-safety: HashMap is not synchronized — use ConcurrentHashMap or external synchronization for concurrent access. |
| 96 | +- Fail-fast: Iterators are fail-fast and will throw ConcurrentModificationException on structural changes. |
| 97 | +- Choosing initial capacity correctly can improve performance and reduce rehashing overhead. |
| 98 | +- Be prepared to explain how hashCode() quality affects map performance and why tree bins were introduced in Java 8. |
| 99 | + |
| 100 | +Summary |
| 101 | +------- |
| 102 | +HashMap is the go-to Map implementation for fast, unordered key-value access in single-threaded or externally synchronized contexts. Understanding capacity, load factor, collision behavior, and Java 8's tree bin optimization is essential for using HashMap effectively and for explaining its performance characteristics in interviews or design discussions. |
0 commit comments