Skip to content

Commit f103162

Browse files
committed
feat(HashMapOrderDemo): illustrate non-deterministic iteration order in HashMap
What - Added HashMapOrderDemo class. - Demonstrated basic HashMap operations: - put() to insert/update entries. - getOrDefault() for safe retrieval. - Printed the map contents to show iteration order. - Added detailed comments explaining why HashMap does not preserve insertion order. Why - To highlight that HashMap does not guarantee ordering of keys or entries. - Developers often mistakenly assume insertion order is preserved. - Provides guidance on when to use LinkedHashMap or TreeMap instead. How - Created a HashMap<String, Integer>. - Inserted three entries: ("Shubham", 91), ("Bob", 80), ("Akshit", 78). - Retrieved non-existing key with getOrDefault("Vipul", 0). - Updated "Shubham" to new value 92. - Printed the HashMap to observe output order. Logic - Inputs: string keys and integer scores. - Processing: - HashMap places entries into internal buckets using hashCode(). - Iteration traverses buckets, not insertion sequence. - Outputs: - Map print shows contents in an arbitrary, non-guaranteed order. - Example: {Akshit=78, Shubham=92, Bob=80} but actual order may differ. Real-life applications - HashMap → use when you need fast key-based lookups and order doesn’t matter. - LinkedHashMap → use when insertion order or access-order must be preserved. - TreeMap → use when you need keys sorted by natural ordering or a custom Comparator. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 30c9b64 commit f103162

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import java.util.HashMap;
2+
3+
public class HashMapOrderDemo {
4+
public static void main(String[] args) {
5+
6+
// Create a HashMap (default capacity 16, load factor 0.75)
7+
HashMap<String, Integer> hashMap = new HashMap<>();
8+
9+
// Put elements into the HashMap
10+
hashMap.put("Shubham", 91);
11+
hashMap.put("Bob", 80);
12+
hashMap.put("Akshit", 78);
13+
14+
// getOrDefault: returns value if key exists; otherwise returns default (0 here).
15+
Integer res = hashMap.getOrDefault("Vipul", 0);
16+
17+
// Update the value for "Shubham" (replaces old value).
18+
hashMap.put("Shubham", 92);
19+
20+
// Print HashMap
21+
System.out.println("HashMap contents: " + hashMap);
22+
23+
/*
24+
* Important: HashMap does NOT guarantee order of keys.
25+
* Why?
26+
* - Internally, entries are stored in buckets (based on hashCode).
27+
* - Iteration goes bucket by bucket, not in insertion order.
28+
* - Therefore, output order may look random, may differ across runs or JDK versions.
29+
30+
* Example Output:
31+
* {Akshit=78, Shubham=92, Bob=80}
32+
33+
* But order is NOT reliable or predictable.
34+
*/
35+
}
36+
}
37+
38+
/* Theory: HashMap Order
39+
40+
1. HashMap uses hashing → elements go into buckets based on hashCode().
41+
2. Because bucket placement is not related to insertion sequence,
42+
iteration does NOT preserve insertion order.
43+
3. If you need guaranteed order:
44+
- Use LinkedHashMap (insertion-order or access-order).
45+
- Or use TreeMap (sorted order).
46+
*/

0 commit comments

Comments
 (0)