Merge pull request #11780 from mayank4all/BAEL-5351
Bael 5351: Added empty map initialization methods in Java 8 & java 9
This commit is contained in:
commit
39c8314079
|
@ -0,0 +1,56 @@
|
||||||
|
package com.baeldung.maps.initialize;
|
||||||
|
|
||||||
|
import com.google.common.collect.ImmutableMap;
|
||||||
|
import com.google.common.collect.Maps;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.LinkedHashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.NavigableMap;
|
||||||
|
import java.util.SortedMap;
|
||||||
|
import java.util.TreeMap;
|
||||||
|
|
||||||
|
public class EmptyMapInitializer {
|
||||||
|
|
||||||
|
public static Map<String, String> articleMap;
|
||||||
|
static {
|
||||||
|
articleMap = new HashMap<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Map<String, String> createEmptyMap() {
|
||||||
|
return Collections.emptyMap();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void createMapUsingConstructors() {
|
||||||
|
Map hashMap = new HashMap();
|
||||||
|
Map linkedHashMap = new LinkedHashMap();
|
||||||
|
Map treeMap = new TreeMap();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Map<String, String> createEmptyMapUsingMapsObject() {
|
||||||
|
Map<String, String> emptyMap = Maps.newHashMap();
|
||||||
|
return emptyMap;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Map createGenericEmptyMapUsingMapsObject() {
|
||||||
|
Map genericEmptyMap = Maps.<String, Integer>newHashMap();
|
||||||
|
return genericEmptyMap;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Map<String, String> createMapUsingGuava() {
|
||||||
|
Map<String, String> emptyMapUsingGuava =
|
||||||
|
Maps.newHashMap(ImmutableMap.of());
|
||||||
|
return emptyMapUsingGuava;
|
||||||
|
}
|
||||||
|
|
||||||
|
public SortedMap<String, String> createEmptySortedMap() {
|
||||||
|
SortedMap<String, String> sortedMap = Collections.emptySortedMap();
|
||||||
|
return sortedMap;
|
||||||
|
}
|
||||||
|
|
||||||
|
public NavigableMap<String, String> createEmptyNavigableMap() {
|
||||||
|
NavigableMap<String, String> navigableMap = Collections.emptyNavigableMap();
|
||||||
|
return navigableMap;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1,31 @@
|
||||||
|
package com.baeldung.maps.initialize;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
|
public class EmptyMapInitializerUnitTest {
|
||||||
|
|
||||||
|
@Test(expected=UnsupportedOperationException.class)
|
||||||
|
public void givenEmptyMap_whenAddingEntries_throwsException() {
|
||||||
|
Map<String, String> map = EmptyMapInitializer.createEmptyMap();
|
||||||
|
map.put("key", "value");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenEmptyMap_whenChecked_returnsTrue() {
|
||||||
|
assertTrue(EmptyMapInitializer.articleMap.isEmpty());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenEmptyMap_whenCreatedUsingGuava_returnsEmptyOrNot() {
|
||||||
|
Map<String, String> emptyMapUsingGuava =
|
||||||
|
EmptyMapInitializer.createMapUsingGuava();
|
||||||
|
assertTrue(emptyMapUsingGuava.isEmpty());
|
||||||
|
emptyMapUsingGuava.put("key", "value");
|
||||||
|
assertFalse(emptyMapUsingGuava.isEmpty());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue