Skip to content

Commit 29b1b07

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

1 file changed

+1
-4
lines changed

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

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

@@ -9,14 +8,12 @@ public static void main(String[] args) {
98
map.put(31,"Smith");
109
map.put(11,"Shubham");
1110
map.put(2,"John");
12-
1311
System.out.println(map);
1412

1513
String student = map.get(3);
1614
System.out.println(student);
1715

1816
map.get(6);
19-
2017
System.out.println(map.containsKey(2));
2118

2219
System.out.println(map.containsValue("John"));
@@ -26,4 +23,4 @@ public static void main(String[] args) {
2623
System.out.println(map);
2724
}
2825
}
29-
}
26+
}

0 commit comments

Comments
 (0)