From d2332a5c1096d0fcfd0c853c59cc58f5389ad4a5 Mon Sep 17 00:00:00 2001 From: Mayank Agarwal Date: Sat, 5 Feb 2022 11:52:40 +0530 Subject: [PATCH 1/6] BAEL-5351: Added empty map initialization methods in Java --- .../map/initialize/EmptyMapInitializer.java | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 core-java-modules/core-java-collections-maps-2/src/main/java/com/baeldung/map/initialize/EmptyMapInitializer.java diff --git a/core-java-modules/core-java-collections-maps-2/src/main/java/com/baeldung/map/initialize/EmptyMapInitializer.java b/core-java-modules/core-java-collections-maps-2/src/main/java/com/baeldung/map/initialize/EmptyMapInitializer.java new file mode 100644 index 0000000000..bef6fa5374 --- /dev/null +++ b/core-java-modules/core-java-collections-maps-2/src/main/java/com/baeldung/map/initialize/EmptyMapInitializer.java @@ -0,0 +1,49 @@ +package com.baeldung.map.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.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() { + return Maps.newHashMap(); + } + + public static Map createMapUsingGuava() { + Map emptyMapUsingGuava = + Maps.newHashMap(ImmutableMap.of()); + return emptyMapUsingGuava; + } + + public NavigableMap createEmptyNavigableMap() { + NavigableMap navigableMap = Collections.emptyNavigableMap(); + return navigableMap; + } + +} From 2e8381fbd7be33938bf2b0f4c14aee8bd232524e Mon Sep 17 00:00:00 2001 From: Mayank Agarwal Date: Sat, 5 Feb 2022 11:53:37 +0530 Subject: [PATCH 2/6] BAEL-5351: Added unit tests for empty map methods --- .../initialize/EmptyMapInitializerTest.java | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 core-java-modules/core-java-collections-maps-2/src/test/java/com/baeldung/map/initialize/EmptyMapInitializerTest.java diff --git a/core-java-modules/core-java-collections-maps-2/src/test/java/com/baeldung/map/initialize/EmptyMapInitializerTest.java b/core-java-modules/core-java-collections-maps-2/src/test/java/com/baeldung/map/initialize/EmptyMapInitializerTest.java new file mode 100644 index 0000000000..07605cdd45 --- /dev/null +++ b/core-java-modules/core-java-collections-maps-2/src/test/java/com/baeldung/map/initialize/EmptyMapInitializerTest.java @@ -0,0 +1,30 @@ +package com.baeldung.map.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 EmptyMapInitializerTest { + + @Test(expected=UnsupportedOperationException.class) + public void givenEmptyMap_whenAddingEntries_throwsException() { + Map map = EmptyMapInitializer.createEmptyMap(); + map.put("key", "value"); + } + + @Test + public void checkStaticMap_isEmpty() { + assertTrue(EmptyMapInitializer.articleMap.isEmpty()); + } + + @Test + public void emptyMapCreated_usingGuava() { + Map emptyMapUsingGuava = EmptyMapInitializer.createMapUsingGuava(); + assertTrue(emptyMapUsingGuava.isEmpty()); + emptyMapUsingGuava.put("key", "value"); + assertFalse(emptyMapUsingGuava.isEmpty()); + } + +} From 7f88e21e92cb60ff14efc637f14cee0ca06c4ea1 Mon Sep 17 00:00:00 2001 From: Mayank Agarwal Date: Sat, 5 Feb 2022 11:56:54 +0530 Subject: [PATCH 3/6] BAEKL-5351: Added method for empty sorted map --- .../com/baeldung/map/initialize/EmptyMapInitializer.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/core-java-modules/core-java-collections-maps-2/src/main/java/com/baeldung/map/initialize/EmptyMapInitializer.java b/core-java-modules/core-java-collections-maps-2/src/main/java/com/baeldung/map/initialize/EmptyMapInitializer.java index bef6fa5374..06185a0f93 100644 --- a/core-java-modules/core-java-collections-maps-2/src/main/java/com/baeldung/map/initialize/EmptyMapInitializer.java +++ b/core-java-modules/core-java-collections-maps-2/src/main/java/com/baeldung/map/initialize/EmptyMapInitializer.java @@ -7,6 +7,7 @@ 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 { @@ -41,6 +42,11 @@ public class EmptyMapInitializer { return emptyMapUsingGuava; } + public SortedMap createEmptySortedMap() { + SortedMap sortedMap = Collections.emptySortedMap(); + return sortedMap; + } + public NavigableMap createEmptyNavigableMap() { NavigableMap navigableMap = Collections.emptyNavigableMap(); return navigableMap; From 54f43a311a223c7e529ad372a2bd4fa9d891d99a Mon Sep 17 00:00:00 2001 From: Mayank Agarwal Date: Sat, 5 Feb 2022 12:03:34 +0530 Subject: [PATCH 4/6] BAEL-5351: Resolved PMD violations by renaming file --- ...MapInitializerTest.java => EmptyMapInitializerUnitTest.java} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename core-java-modules/core-java-collections-maps-2/src/test/java/com/baeldung/map/initialize/{EmptyMapInitializerTest.java => EmptyMapInitializerUnitTest.java} (95%) diff --git a/core-java-modules/core-java-collections-maps-2/src/test/java/com/baeldung/map/initialize/EmptyMapInitializerTest.java b/core-java-modules/core-java-collections-maps-2/src/test/java/com/baeldung/map/initialize/EmptyMapInitializerUnitTest.java similarity index 95% rename from core-java-modules/core-java-collections-maps-2/src/test/java/com/baeldung/map/initialize/EmptyMapInitializerTest.java rename to core-java-modules/core-java-collections-maps-2/src/test/java/com/baeldung/map/initialize/EmptyMapInitializerUnitTest.java index 07605cdd45..82225a46ec 100644 --- a/core-java-modules/core-java-collections-maps-2/src/test/java/com/baeldung/map/initialize/EmptyMapInitializerTest.java +++ b/core-java-modules/core-java-collections-maps-2/src/test/java/com/baeldung/map/initialize/EmptyMapInitializerUnitTest.java @@ -6,7 +6,7 @@ import org.junit.Test; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; -public class EmptyMapInitializerTest { +public class EmptyMapInitializerUnitTest { @Test(expected=UnsupportedOperationException.class) public void givenEmptyMap_whenAddingEntries_throwsException() { From b35d0ea7c4481c5663487bd9e12a3a7eba0eb3ce Mon Sep 17 00:00:00 2001 From: Mayank Agarwal Date: Tue, 8 Feb 2022 00:40:07 +0530 Subject: [PATCH 5/6] BAEL-5351: Unit Tests name as per BBD convention --- .../map/initialize/EmptyMapInitializerUnitTest.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/core-java-modules/core-java-collections-maps-2/src/test/java/com/baeldung/map/initialize/EmptyMapInitializerUnitTest.java b/core-java-modules/core-java-collections-maps-2/src/test/java/com/baeldung/map/initialize/EmptyMapInitializerUnitTest.java index 82225a46ec..b40ffed94b 100644 --- a/core-java-modules/core-java-collections-maps-2/src/test/java/com/baeldung/map/initialize/EmptyMapInitializerUnitTest.java +++ b/core-java-modules/core-java-collections-maps-2/src/test/java/com/baeldung/map/initialize/EmptyMapInitializerUnitTest.java @@ -15,13 +15,14 @@ public class EmptyMapInitializerUnitTest { } @Test - public void checkStaticMap_isEmpty() { + public void givenEmptyMap_whenChecked_returnsTrue() { assertTrue(EmptyMapInitializer.articleMap.isEmpty()); } @Test - public void emptyMapCreated_usingGuava() { - Map emptyMapUsingGuava = EmptyMapInitializer.createMapUsingGuava(); + public void givenEmptyMap_whenCreatedUsingGuava_returnsEmptyOrNot() { + Map emptyMapUsingGuava = + EmptyMapInitializer.createMapUsingGuava(); assertTrue(emptyMapUsingGuava.isEmpty()); emptyMapUsingGuava.put("key", "value"); assertFalse(emptyMapUsingGuava.isEmpty()); From 7995f7d7c52963a557bca75e210842299464b89a Mon Sep 17 00:00:00 2001 From: Mayank Agarwal Date: Sun, 13 Feb 2022 16:43:59 +0530 Subject: [PATCH 6/6] BAEL-5351: Moved to core-java-collections-4 --- .../com/baeldung/maps}/initialize/EmptyMapInitializer.java | 5 +++-- .../maps}/initialize/EmptyMapInitializerUnitTest.java | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) rename core-java-modules/{core-java-collections-maps-2/src/main/java/com/baeldung/map => core-java-collections-4/src/main/java/com/baeldung/maps}/initialize/EmptyMapInitializer.java (91%) rename core-java-modules/{core-java-collections-maps-2/src/test/java/com/baeldung/map => core-java-collections-4/src/test/java/com/baeldung/maps}/initialize/EmptyMapInitializerUnitTest.java (95%) diff --git a/core-java-modules/core-java-collections-maps-2/src/main/java/com/baeldung/map/initialize/EmptyMapInitializer.java b/core-java-modules/core-java-collections-4/src/main/java/com/baeldung/maps/initialize/EmptyMapInitializer.java similarity index 91% rename from core-java-modules/core-java-collections-maps-2/src/main/java/com/baeldung/map/initialize/EmptyMapInitializer.java rename to core-java-modules/core-java-collections-4/src/main/java/com/baeldung/maps/initialize/EmptyMapInitializer.java index 06185a0f93..3dc644f1af 100644 --- a/core-java-modules/core-java-collections-maps-2/src/main/java/com/baeldung/map/initialize/EmptyMapInitializer.java +++ b/core-java-modules/core-java-collections-4/src/main/java/com/baeldung/maps/initialize/EmptyMapInitializer.java @@ -1,4 +1,4 @@ -package com.baeldung.map.initialize; +package com.baeldung.maps.initialize; import com.google.common.collect.ImmutableMap; import com.google.common.collect.Maps; @@ -33,7 +33,8 @@ public class EmptyMapInitializer { } public Map createGenericEmptyMapUsingMapsObject() { - return Maps.newHashMap(); + Map genericEmptyMap = Maps.newHashMap(); + return genericEmptyMap; } public static Map createMapUsingGuava() { diff --git a/core-java-modules/core-java-collections-maps-2/src/test/java/com/baeldung/map/initialize/EmptyMapInitializerUnitTest.java b/core-java-modules/core-java-collections-4/src/test/java/com/baeldung/maps/initialize/EmptyMapInitializerUnitTest.java similarity index 95% rename from core-java-modules/core-java-collections-maps-2/src/test/java/com/baeldung/map/initialize/EmptyMapInitializerUnitTest.java rename to core-java-modules/core-java-collections-4/src/test/java/com/baeldung/maps/initialize/EmptyMapInitializerUnitTest.java index b40ffed94b..cc25205ba7 100644 --- a/core-java-modules/core-java-collections-maps-2/src/test/java/com/baeldung/map/initialize/EmptyMapInitializerUnitTest.java +++ b/core-java-modules/core-java-collections-4/src/test/java/com/baeldung/maps/initialize/EmptyMapInitializerUnitTest.java @@ -1,4 +1,4 @@ -package com.baeldung.map.initialize; +package com.baeldung.maps.initialize; import java.util.Map; import org.junit.Test;