Commit 29b1b07
committed
feat(HashMapDemo): add basic HashMap operations demo
What
- Implemented a simple `HashMapDemo` showcasing common operations with `HashMap<Integer, String>`.
- Demonstrates insertion, lookup, key/value presence checks, and iteration.
Why
- To illustrate how `HashMap` provides constant-time average complexity for insert/get/contains operations.
- Helps understand differences between key-based vs. value-based lookups.
- Shows iteration over keys using `keySet()`.
How
1. Created a `HashMap<Integer, String>` to map student IDs → names.
2. Inserted three entries with `put()`.
3. Retrieved values using `get(key)` (returns `null` if key not present).
4. Checked existence with `containsKey()` and `containsValue()`.
5. Iterated over all keys via `map.keySet()` inside an enhanced for-loop.
Logic
- `put(31,"Smith")`, `put(11,"Shubham")`, `put(2,"John")` inserts mappings.
- `map.get(3)` returns `null` since key `3` does not exist.
- `map.containsKey(2)` returns `true`.
- `map.containsValue("John")` returns `true`.
- Iteration prints the entire `map` object for each key (demonstrates `toString()` representation).
Real-life applications
- `HashMap` is one of the most widely used collections in Java.
- Used for implementing caches, lookup tables, dictionaries, and frequency counters.
- O(1) average-time operations make it efficient for quick key-based access.
Notes
- `HashMap` allows **null keys and multiple null values** (unlike `Hashtable`).
- It is **not synchronized** — for concurrent use, prefer `ConcurrentHashMap`.
- Iteration order is **not guaranteed** (use `LinkedHashMap` if order matters).
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 9986675 commit 29b1b07
File tree
1 file changed
+1
-4
lines changed- Section 25 Collections Frameworks/Map Interface/HashMap/src
1 file changed
+1
-4
lines changedLines changed: 1 addition & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
2 | 1 | | |
3 | 2 | | |
4 | 3 | | |
| |||
9 | 8 | | |
10 | 9 | | |
11 | 10 | | |
12 | | - | |
13 | 11 | | |
14 | 12 | | |
15 | 13 | | |
16 | 14 | | |
17 | 15 | | |
18 | 16 | | |
19 | | - | |
20 | 17 | | |
21 | 18 | | |
22 | 19 | | |
| |||
26 | 23 | | |
27 | 24 | | |
28 | 25 | | |
29 | | - | |
| 26 | + | |
0 commit comments