JAVA-11382 Update article Create an empty map in Java (#12383)

This commit is contained in:
anuragkumawat 2022-06-20 18:46:31 +05:30 committed by GitHub
parent 150bdb070b
commit 033ddb7a41
2 changed files with 11 additions and 1 deletions

View File

@ -32,7 +32,7 @@ public class EmptyMapInitializer {
return emptyMap;
}
public Map createGenericEmptyMapUsingMapsObject() {
public Map createGenericEmptyMapUsingGuavaMapsObject() {
Map genericEmptyMap = Maps.<String, Integer>newHashMap();
return genericEmptyMap;
}
@ -43,6 +43,11 @@ public class EmptyMapInitializer {
return emptyMapUsingGuava;
}
public static Map<String, String> createImmutableMapUsingGuava() {
Map<String, String> emptyImmutableMapUsingGuava = ImmutableMap.of();
return emptyImmutableMapUsingGuava;
}
public SortedMap<String, String> createEmptySortedMap() {
SortedMap<String, String> sortedMap = Collections.emptySortedMap();
return sortedMap;

View File

@ -28,4 +28,9 @@ public class EmptyMapInitializerUnitTest {
assertFalse(emptyMapUsingGuava.isEmpty());
}
@Test(expected=UnsupportedOperationException.class)
public void givenImmutableEmptyMapUsingGuava_whenAddingEntries_throwsException() {
Map<String, String> map = EmptyMapInitializer.createImmutableMapUsingGuava();
map.put("key", "value");
}
}