JAVA-11382 Update article Create an empty map in Java (#12383)
This commit is contained in:
parent
150bdb070b
commit
033ddb7a41
@ -32,7 +32,7 @@ public class EmptyMapInitializer {
|
|||||||
return emptyMap;
|
return emptyMap;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Map createGenericEmptyMapUsingMapsObject() {
|
public Map createGenericEmptyMapUsingGuavaMapsObject() {
|
||||||
Map genericEmptyMap = Maps.<String, Integer>newHashMap();
|
Map genericEmptyMap = Maps.<String, Integer>newHashMap();
|
||||||
return genericEmptyMap;
|
return genericEmptyMap;
|
||||||
}
|
}
|
||||||
@ -43,6 +43,11 @@ public class EmptyMapInitializer {
|
|||||||
return emptyMapUsingGuava;
|
return emptyMapUsingGuava;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static Map<String, String> createImmutableMapUsingGuava() {
|
||||||
|
Map<String, String> emptyImmutableMapUsingGuava = ImmutableMap.of();
|
||||||
|
return emptyImmutableMapUsingGuava;
|
||||||
|
}
|
||||||
|
|
||||||
public SortedMap<String, String> createEmptySortedMap() {
|
public SortedMap<String, String> createEmptySortedMap() {
|
||||||
SortedMap<String, String> sortedMap = Collections.emptySortedMap();
|
SortedMap<String, String> sortedMap = Collections.emptySortedMap();
|
||||||
return sortedMap;
|
return sortedMap;
|
||||||
|
@ -28,4 +28,9 @@ public class EmptyMapInitializerUnitTest {
|
|||||||
assertFalse(emptyMapUsingGuava.isEmpty());
|
assertFalse(emptyMapUsingGuava.isEmpty());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test(expected=UnsupportedOperationException.class)
|
||||||
|
public void givenImmutableEmptyMapUsingGuava_whenAddingEntries_throwsException() {
|
||||||
|
Map<String, String> map = EmptyMapInitializer.createImmutableMapUsingGuava();
|
||||||
|
map.put("key", "value");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user