diff --git a/core-java-modules/core-java-collections-maps-8/src/main/java/com/baeldung/map/maptruevaluecheck/MapValuesCheck.java b/core-java-modules/core-java-collections-maps-8/src/main/java/com/baeldung/map/maptruevaluecheck/MapValuesCheck.java new file mode 100644 index 000000000000..04d11b8a8f6e --- /dev/null +++ b/core-java-modules/core-java-collections-maps-8/src/main/java/com/baeldung/map/maptruevaluecheck/MapValuesCheck.java @@ -0,0 +1,38 @@ +package com.baeldung.map.maptruevaluecheck; + +import java.util.Map; +import java.util.HashSet; +import java.util.Set; + +public class MapValuesCheck { + + public static boolean areAllValuesSameWithReduce(Map map) { + if (map.isEmpty()) return true; + int firstValue = map.values().iterator().next(); + return map.values().stream().reduce(true, + (result, value) -> result && value.equals(firstValue), + Boolean::logicalAnd); + } + + public static boolean areAllValuesSameWithSet(Map map) { + Set uniqueValues = new HashSet<>(map.values()); + return uniqueValues.size() == 1; + } + + public static boolean areAllValuesSameWithAllMatch(Map map) { + if (map.isEmpty()) return true; + int firstValue = map.values().iterator().next(); + return map.values().stream().allMatch(value -> value.equals(firstValue)); + } + + public static boolean areAllValuesSameWithLoop(Map map) { + if (map.isEmpty()) return true; + int firstValue = map.values().iterator().next(); + for (int value : map.values()) { + if (value != firstValue) { + return false; + } + } + return true; + } +} \ No newline at end of file diff --git a/core-java-modules/core-java-collections-maps-8/src/test/java/com/baeldung/map/maptruevaluecheck/MapValuesCheckUnitTest.java b/core-java-modules/core-java-collections-maps-8/src/test/java/com/baeldung/map/maptruevaluecheck/MapValuesCheckUnitTest.java new file mode 100644 index 000000000000..81118ea24b4a --- /dev/null +++ b/core-java-modules/core-java-collections-maps-8/src/test/java/com/baeldung/map/maptruevaluecheck/MapValuesCheckUnitTest.java @@ -0,0 +1,49 @@ +package com.baeldung.map.maptruevaluecheck; + +import org.junit.jupiter.api.Test; +import java.util.Map; +import static org.junit.jupiter.api.Assertions.*; + +public class MapValuesCheckUnitTest { + + @Test + public void givenMapWithAllSameValues_whenCheckAllValuesSame_thenReturnTrue() { + Map map = Map.of("English", 100, "Maths", 100, "Science", 100); + + assertTrue(MapValuesCheck.areAllValuesSameWithAllMatch(map)); + assertTrue(MapValuesCheck.areAllValuesSameWithReduce(map)); + assertTrue(MapValuesCheck.areAllValuesSameWithLoop(map)); + } + + @Test + public void givenMapWithDifferentValues_whenCheckAllValuesSame_thenReturnFalse() { + Map map = Map.of("English", 1000, "Maths", 100, "Science", 100); + + assertFalse(MapValuesCheck.areAllValuesSameWithAllMatch(map)); + assertFalse(MapValuesCheck.areAllValuesSameWithReduce(map)); + assertFalse(MapValuesCheck.areAllValuesSameWithLoop(map)); + } + + @Test + public void givenEmptyMap_whenCheckAllValuesSame_thenReturnTrue() { + Map map = Map.of(); + + assertTrue(MapValuesCheck.areAllValuesSameWithAllMatch(map)); + assertTrue(MapValuesCheck.areAllValuesSameWithReduce(map)); + assertTrue(MapValuesCheck.areAllValuesSameWithLoop(map)); + } + + @Test + void givenMapWithAllSameValues_whenCheckingAllValuesSameWithSet_thenReturnTrue() { + Map map = Map.of("English", 100, "Maths", 100, "Science", 100); + + assertTrue(MapValuesCheck.areAllValuesSameWithSet(map)); + } + + @Test + void givenMapWithDifferentValues_whenCheckingAllValuesSameWithSet_thenReturnFalse() { + Map map = Map.of("English", 1000, "Maths", 100, "Science", 100); + + assertFalse(MapValuesCheck.areAllValuesSameWithSet(map)); + } +} \ No newline at end of file