Skip to content

Commit 9dda328

Browse files
committed
feat(HashMapDemo3): add HashMap entrySet iteration and formatted printing demo
What - Implemented `HashMapDemo3` to show how to retrieve entries from a `HashMap` using `entrySet()` and iterate over them with formatted output. - Added logic to print both the original map and processed key–value pairs. Why - Demonstrates a more structured way of accessing `HashMap` contents (key + value together). - Highlights the fact that `HashMap` does not maintain insertion or sorted order. - Provides an example of formatting values (e.g., uppercase transformation) without modifying the original map. How 1. Created a `HashMap<Integer, String>` and inserted sample entries (`Jack`, `Smith`, `Shubham`, `John`). 2. Printed the original map directly. 3. Retrieved entries via `map.entrySet()`. 4. Iterated over each entry: - Printed key and value with formatted width using `printf`. - Printed the uppercase version of the value (demonstrating transformation without changing the map). 5. Printed the final map again to show contents remain unchanged. Notes on ordering - `HashMap` **does not guarantee iteration order**. - The printed order depends on hash distribution and internal bucket structure. - With a small number of elements, output may look "sorted" by coincidence, but this is unreliable. - For predictable ordering: - Use `LinkedHashMap` → preserves insertion order. - Use `TreeMap` → sorts by key. Real-life application - Iterating key–value mappings for reporting, logging, or transforming data (like uppercase conversion). - Useful in scenarios like processing user IDs → names, product IDs → details, etc. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 377d2fd commit 9dda328

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import java.util.HashMap;
2+
import java.util.Map;
3+
import java.util.Set;
4+
5+
public class HashMapDemo3 {
6+
public static void main(String[] args) {
7+
HashMap<Integer, String> map = new HashMap<>();
8+
9+
map.put(1, "Jack");
10+
map.put(31, "Smith");
11+
map.put(11, "Shubham");
12+
map.put(2, "John");
13+
14+
System.out.println("Original Map: " + map);
15+
16+
// List of entries.
17+
Set<Map.Entry<Integer, String>> entries = map.entrySet();
18+
19+
for (Map.Entry<Integer, String> entry : entries) {
20+
System.out.printf("%-3d : %-10s%n", entry.getKey(), entry.getValue());
21+
System.out.printf(" %-10s%n", entry.getValue().toUpperCase());
22+
}
23+
System.out.println("Final Map: " + map);
24+
}
25+
}
26+
27+
/*
28+
The reason the HashMap sometimes looks like it is printing in natural (sorted) order is purely coincidental and
29+
depends on the internal hashing mechanism.
30+
31+
Why does it look sorted?
32+
33+
1. HashMap does not guarantee order
34+
- The order of elements depends on the hash function and how entries are placed into buckets.
35+
36+
2. Small number of elements
37+
- When there are only a few entries, it may sometimes appear sorted by chance,
38+
but this behavior is not reliable.
39+
40+
3. JDK version & HashMap implementation details
41+
- In some Java versions, the way the hashing algorithm and bucket assignments work
42+
can make the output look sorted in certain cases.
43+
- However, this should never be relied upon.
44+
45+
Conclusion:
46+
If you need a predictable order, do not use HashMap.
47+
Use `TreeMap` (sorted by keys) or `LinkedHashMap` (insertion order) instead.
48+
*/

0 commit comments

Comments
 (0)