Skip to content

Commit 9c36860

Browse files
committed
feat(ImmutableMapDemo): demonstrate creating immutable maps in Java
What - Added ImmutableMapDemo class. - Showed 4 approaches: 1. Normal mutable HashMap (map1). 2. Immutable view using Collections.unmodifiableMap() (map2). 3. Immutable map using Map.of() (map3). 4. Immutable map using Map.ofEntries() (map4). - Printed behavior before and after modification attempts. Why - To teach the differences between mutable maps, immutable views, and truly immutable maps in Java. - Highlights caveats of unmodifiableMap (changes in original reflect in the view). - Introduces Java 9+ factory methods Map.of and Map.ofEntries as safer alternatives. How - map1: created mutable HashMap and inserted entries. - map2: wrapped map1 with Collections.unmodifiableMap(). - map3: used Map.of() for up to 10 entries. - map4: used Map.ofEntries() for arbitrary entries. - Demonstrated that modifying map1 reflects in map2, while map3 and map4 reject modifications with UnsupportedOperationException. Logic - Inputs: - Key-value pairs like ("A",1), ("B",2), ("Shubham",98). - Processing: - Map1 accepts updates directly. - Map2 proxies to map1 for reads but blocks direct writes. - Map3 and map4 are created as fixed-size immutable maps. - Outputs: - map1 → reflects all additions. - map2 → mirrors changes in map1 but cannot be modified directly. - map3 → fixed {Shubham=98, Vivek=89}. - map4 → fixed {Akshit=99, Vivek=99, Neha=95}. Real-life applications - Collections.unmodifiableMap → when you need to share a read-only view of a mutable map. - Map.of / Map.ofEntries → when you want fully immutable maps for constants, config data, or safe multi-threaded access. - Immutable collections improve safety by preventing accidental modifications and simplify reasoning in concurrent programs. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent f418078 commit 9c36860

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import java.util.Collections;
2+
import java.util.HashMap;
3+
import java.util.Map;
4+
5+
public class ImmutableMapDemo {
6+
public static void main(String[] args) {
7+
// 1. Normal mutable HashMap
8+
Map<String, Integer> map1 = new HashMap<>();
9+
map1.put("A", 1);
10+
map1.put("B", 2);
11+
12+
System.out.println("Original mutable HashMap (map1): " + map1);
13+
14+
// 2. Collections.unmodifiableMap()
15+
// Creates an "immutable view" of map1.
16+
// You can READ from map2 but cannot MODIFY it.
17+
// Any modification attempt (put, remove) will throw UnsupportedOperationException.
18+
Map<String, Integer> map2 = Collections.unmodifiableMap(map1);
19+
20+
System.out.println("Immutable view created from map1 (map2): " + map2);
21+
22+
// NOTE: If you change map1, map2 also reflects changes (because it's a wrapper, not a deep copy).
23+
map1.put("C", 3);
24+
System.out.println("After modifying map1 -> map1: " + map1);
25+
System.out.println("After modifying map1 -> map2: " + map2); // Reflects the new entry
26+
27+
// map2.put("D", 4); // This will throw UnsupportedOperationException
28+
29+
// 3. Map.of() (Java 9+)
30+
// Directly creates an immutable map.
31+
// Limitation: Can accept only up to 10 entries.
32+
// Once created, it cannot be modified.
33+
Map<String, Integer> map3 = Map.of("Shubham", 98, "Vivek", 89);
34+
35+
System.out.println("Immutable Map created with Map.of(): " + map3);
36+
37+
// map3.put("Akshit", 88); // Throws UnsupportedOperationException
38+
39+
// 4. Map.ofEntries() (Java 9+)
40+
// A more flexible way of creating immutable maps.
41+
// Allows creating with many entries (more than 10 if required).
42+
Map<String, Integer> map4 = Map.ofEntries(
43+
Map.entry("Akshit", 99),
44+
Map.entry("Vivek", 99),
45+
Map.entry("Neha", 95)
46+
);
47+
48+
System.out.println("Immutable Map created with Map.ofEntries(): " + map4);
49+
50+
System.out.println("\nSummary:");
51+
System.out.println("map1 is mutable → can be changed.");
52+
System.out.println("map2 is immutable view of map1 → changes in map1 are reflected, "
53+
+ "but direct modification on map2 is not allowed.");
54+
System.out.println("map3 is truly immutable → no changes allowed (up to 10 entries).");
55+
System.out.println("map4 is truly immutable → no changes allowed, supports >10 entries.");
56+
}
57+
}

0 commit comments

Comments
 (0)