Skip to content

Commit 377d2fd

Browse files
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

1 file changed

+2
-2
lines changed

Section25CollectionFramework/src/HashMapDemo/HashMapDemo2.java renamed to Section 25 Collections Frameworks/Map Interface/HashMap/src/HashMapDemo2.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
package HashMapDemo;
21
import java.util.HashMap;
32

43
public class HashMapDemo2 {
@@ -10,6 +9,7 @@ public static void main(String[] args) {
109
HM.put(3, "This is the Hash Map");
1110
HM.put(4, "Adding elements");
1211
HM.put(5, "Done");
12+
1313
System.out.println(HM);
1414
}
15-
}
15+
}

0 commit comments

Comments
 (0)