BAEL-5351: Moved to core-java-collections-4

This commit is contained in:
Mayank Agarwal
2022-02-13 16:43:59 +05:30
parent 9db7699059
commit 3110bc8c6d
2 changed files with 4 additions and 3 deletions

View File

@@ -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;
}
}

View File

@@ -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());
}
}