diff --git a/core-java-modules/core-java-collections-4/src/main/java/com/baeldung/maps/initialize/EmptyMapInitializer.java b/core-java-modules/core-java-collections-4/src/main/java/com/baeldung/maps/initialize/EmptyMapInitializer.java new file mode 100644 index 0000000000..3dc644f1af --- /dev/null +++ b/core-java-modules/core-java-collections-4/src/main/java/com/baeldung/maps/initialize/EmptyMapInitializer.java @@ -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 articleMap; + static { + articleMap = new HashMap<>(); + } + + public static Map createEmptyMap() { + return Collections.emptyMap(); + } + + public void createMapUsingConstructors() { + Map hashMap = new HashMap(); + Map linkedHashMap = new LinkedHashMap(); + Map treeMap = new TreeMap(); + } + + public Map createEmptyMapUsingMapsObject() { + Map emptyMap = Maps.newHashMap(); + return emptyMap; + } + + public Map createGenericEmptyMapUsingMapsObject() { + Map genericEmptyMap = Maps.newHashMap(); + return genericEmptyMap; + } + + public static Map createMapUsingGuava() { + Map emptyMapUsingGuava = + Maps.newHashMap(ImmutableMap.of()); + return emptyMapUsingGuava; + } + + public SortedMap createEmptySortedMap() { + SortedMap sortedMap = Collections.emptySortedMap(); + return sortedMap; + } + + public NavigableMap createEmptyNavigableMap() { + NavigableMap navigableMap = Collections.emptyNavigableMap(); + return navigableMap; + } + +} diff --git a/core-java-modules/core-java-collections-4/src/test/java/com/baeldung/maps/initialize/EmptyMapInitializerUnitTest.java b/core-java-modules/core-java-collections-4/src/test/java/com/baeldung/maps/initialize/EmptyMapInitializerUnitTest.java new file mode 100644 index 0000000000..cc25205ba7 --- /dev/null +++ b/core-java-modules/core-java-collections-4/src/test/java/com/baeldung/maps/initialize/EmptyMapInitializerUnitTest.java @@ -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 map = EmptyMapInitializer.createEmptyMap(); + map.put("key", "value"); + } + + @Test + public void givenEmptyMap_whenChecked_returnsTrue() { + assertTrue(EmptyMapInitializer.articleMap.isEmpty()); + } + + @Test + public void givenEmptyMap_whenCreatedUsingGuava_returnsEmptyOrNot() { + Map emptyMapUsingGuava = + EmptyMapInitializer.createMapUsingGuava(); + assertTrue(emptyMapUsingGuava.isEmpty()); + emptyMapUsingGuava.put("key", "value"); + assertFalse(emptyMapUsingGuava.isEmpty()); + } + +}