GeezerVison is showing github.com — JAVA-11382 Update article Create an empty map in Java (#12383) · FEDENG01/tutorials@95d381a
File tree
Expand file treeCollapse file tree
Open diff view settings
core-java-modules/core-java-collections-4/src main/java/com/baeldung/maps/initialize test/java/com/baeldung/maps/initialize
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: core-java-modules/core-java-collections-4/src/main/java/com/baeldung/maps/initialize/EmptyMapInitializer.java +6-1Lines changed: 6 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -32,7 +32,7 @@ public Map<String, String> createEmptyMapUsingMapsObject() { |
32 | 32 | return emptyMap; |
33 | 33 | } |
34 | 34 |
|
35 | | - public Map createGenericEmptyMapUsingMapsObject() { |
| 35 | + public Map createGenericEmptyMapUsingGuavaMapsObject() { |
36 | 36 | Map genericEmptyMap = Maps.<String, Integer>newHashMap(); |
37 | 37 | return genericEmptyMap; |
38 | 38 | } |
@@ -43,6 +43,11 @@ public static Map<String, String> createMapUsingGuava() { |
43 | 43 | return emptyMapUsingGuava; |
44 | 44 | } |
45 | 45 |
|
| 46 | + public static Map<String, String> createImmutableMapUsingGuava() { |
| 47 | + Map<String, String> emptyImmutableMapUsingGuava = ImmutableMap.of(); |
| 48 | + return emptyImmutableMapUsingGuava; |
| 49 | + } |
| 50 | + |
46 | 51 | public SortedMap<String, String> createEmptySortedMap() { |
47 | 52 | SortedMap<String, String> sortedMap = Collections.emptySortedMap(); |
48 | 53 | return sortedMap; |
|
Collapse file
Copy file name to clipboardExpand all lines: core-java-modules/core-java-collections-4/src/test/java/com/baeldung/maps/initialize/EmptyMapInitializerUnitTest.java +5Lines changed: 5 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -28,4 +28,9 @@ public void givenEmptyMap_whenCreatedUsingGuava_returnsEmptyOrNot() { |
28 | 28 | assertFalse(emptyMapUsingGuava.isEmpty()); |
29 | 29 | } |
30 | 30 |
|
| 31 | + @Test(expected=UnsupportedOperationException.class) |
| 32 | + public void givenImmutableEmptyMapUsingGuava_whenAddingEntries_throwsException() { |
| 33 | + Map<String, String> map = EmptyMapInitializer.createImmutableMapUsingGuava(); |
| 34 | + map.put("key", "value"); |
| 35 | + } |
31 | 36 | } |