Commit 377d2fd
committed
feat(HashMapDemo2): add simple HashMap insertion and print demo
What
- Implemented a minimal `HashMapDemo2` that shows how to add entries into a `HashMap` and print its contents.
Why
- To demonstrate the basic usage of `HashMap` for storing key–value pairs.
- Provides a quick example of how insertion works and how the default `toString()` shows map contents.
How
1. Created a `HashMap<Integer, String>` called `HM`.
2. Inserted five entries using `put(key, value)`.
3. Printed the map directly with `System.out.println(HM)`.
Logic
- Keys: `1 → 5` (integers).
- Values: `"Hello"`, `"World"`, `"This is the Hash Map"`, `"Adding elements"`, `"Done"`.
- `HashMap` internally uses hashing to determine bucket placement.
- Printing shows all entries in `{key=value, ...}` format, but order is **not guaranteed** (depends on hash distribution).
Real-life applications
- Basic key–value storage: e.g., mapping user IDs to user names, student roll numbers to details, or product IDs to descriptions.
- Fast lookups (average O(1) time complexity).
Notes
- `HashMap` allows **null keys** (only one) and **multiple null values**.
- Not synchronized — use `ConcurrentHashMap` in multithreaded environments.
- If insertion order is important, use `LinkedHashMap`.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 29b1b07 commit 377d2fd
File tree
1 file changed
+2
-2
lines changed- Section 25 Collections Frameworks/Map Interface/HashMap/src
1 file changed
+2
-2
lines changedLines changed: 2 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
2 | 1 | | |
3 | 2 | | |
4 | 3 | | |
| |||
10 | 9 | | |
11 | 10 | | |
12 | 11 | | |
| 12 | + | |
13 | 13 | | |
14 | 14 | | |
15 | | - | |
| 15 | + | |
0 commit comments