Commit 850981a
committed
feat(HashMapCustom): add lightweight custom HashMap implementation with iterable support
What
- Added HashMapCustom<K,V> core implementation with:
- Entry<K,V> inner class (key, value, toString).
- Basic operations: put(K,V), get(K), remove(K), size(), printMap().
- Automatic resizing with rehash when load factor threshold exceeded.
- Configurable initial capacity and load factor (constructors + setLoadFactor()).
- Iterable support: implements Iterable<Entry<K,V>> with an internal iterator that walks buckets in order.
Why
- Provide an educational, minimal reimplementation of a hash-based map to illustrate:
- Hashing → bucket index calculation using hashCode and modulo.
- Collision handling via separate chaining (LinkedList per bucket).
- Rehashing/resizing strategy when load factor is exceeded.
- How iteration across buckets and linked lists can be implemented.
- Useful for teaching, debugging, or comparing behavior with java.util.HashMap in a controlled, transparent way.
How
- Buckets are an array of LinkedList<Entry<K,V>>.
- getBucketIndex(key) computes Math.abs(key.hashCode()) % buckets.length.
- put:
- Locate bucket, create list if null.
- If key exists, update value.
- Else add new Entry and increment size.
- After insert, check load factor and call resize() if needed.
- get:
- Locate bucket and scan entries, returning matching value or null.
- remove:
- Locate bucket, remove matching entry and decrement size.
- resize:
- Double bucket array length, rehash all existing entries by calling put() on each (size reset and recalculated).
- printMap:
- Iterate bucket array and print entries per bucket.
- Iterable:
- Custom HashMapIterator advances to next non-empty bucket and iterates its linked list; hasNext() peeks ahead to future buckets.
Logic
- Inputs: keys/values provided to put/get/remove.
- Outputs: stored values retrievable by get, printed bucket layout via printMap.
- Flow:
1. Compute bucket index from key.
2. Use separate chaining (LinkedList) to store entries for that index.
3. Maintain size and trigger resize when (double)size / buckets.length > loadFactor.
4. Iterator moves bucket-by-bucket and iterates each non-empty LinkedList.
- Edge cases handled:
- Null buckets lazily created.
- Key equality uses entry.key.equals(key).
- Resize rehashes entries into newly sized bucket array.
- Iterator throws NoSuchElementException when exhausted.
- Complexity:
- Average put/get/remove are O(1) amortized; resize is O(n).
- Iterator traverses all buckets and entries O(n).
- Concurrency / thread-safety:
- Implementation is not thread-safe; concurrent modifications may break behavior.
- Error handling:
- setLoadFactor rejects non-positive values with IllegalArgumentException.
Real-life applications
- Educational playground for students learning how hash maps work internally.
- Small-scale custom mapping needs where controlling hashing/rehash behavior or instrumentation is required.
- Basis for experiments: alternative collision strategies, probing, custom hashing, or instrumentation for teaching.
Notes
- Uses LinkedList chaining for simplicity; production HashMap uses optimized treeification for large buckets.
- Buckets array created with raw type LinkedList[]; generics-aware creation is avoided for clarity but may be improved using @SuppressWarnings or Lists of lists.
- Using Math.abs(key.hashCode()) may still be negative in one edge case (Integer.MIN_VALUE); consider handling that to avoid negative indices.
- Rehashing uses put() which increments size; resize resets size to 0 before re-inserting to recalculate correctly.
- The iterator implementation advances to next non-empty bucket lazily; it is fail-fast behavior is not implemented (no modCount).
Signed-off-by: https://github.com/Someshdiwan <someshdiwan369@gmail.com>1 parent 382a0a7 commit 850981a
1 file changed
Lines changed: 11 additions & 53 deletions
File tree
- Section 25 Collections Frameworks/Map Interface/HashMap/Custom HashMap/src/CustomHashMap
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
2 | | - | |
3 | | - | |
4 | | - | |
5 | | - | |
6 | | - | |
7 | | - | |
8 | | - | |
9 | | - | |
10 | | - | |
11 | | - | |
| 1 | + | |
12 | 2 | | |
13 | 3 | | |
14 | 4 | | |
15 | 5 | | |
16 | 6 | | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
17 | 16 | | |
18 | 17 | | |
19 | 18 | | |
| |||
97 | 96 | | |
98 | 97 | | |
99 | 98 | | |
100 | | - | |
| 99 | + | |
101 | 100 | | |
102 | 101 | | |
103 | 102 | | |
| |||
213 | 212 | | |
214 | 213 | | |
215 | 214 | | |
216 | | - | |
217 | | - | |
218 | | - | |
219 | | - | |
220 | | - | |
221 | | - | |
222 | | - | |
223 | | - | |
224 | | - | |
225 | | - | |
226 | | - | |
227 | | - | |
228 | | - | |
229 | | - | |
230 | | - | |
231 | | - | |
232 | | - | |
233 | | - | |
234 | | - | |
235 | | - | |
236 | | - | |
237 | | - | |
238 | | - | |
239 | | - | |
240 | 215 | | |
241 | | - | |
242 | | - | |
243 | | - | |
244 | | - | |
245 | | - | |
246 | | - | |
247 | | - | |
248 | | - | |
249 | | - | |
250 | | - | |
251 | | - | |
252 | | - | |
253 | | - | |
254 | | - | |
255 | | - | |
256 | | - | |
257 | | - | |
0 commit comments