这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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<String, Integer> 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<String, Integer> map) {
Set<Integer> uniqueValues = new HashSet<>(map.values());
return uniqueValues.size() == 1;
}

public static boolean areAllValuesSameWithAllMatch(Map<String, Integer> 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<String, Integer> map) {
if (map.isEmpty()) return true;
int firstValue = map.values().iterator().next();
for (int value : map.values()) {
if (value != firstValue) {
return false;
}
}
return true;
}
}
Original file line number Diff line number Diff line change
@@ -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<String, Integer> 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<String, Integer> 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<String, Integer> map = Map.of();

assertTrue(MapValuesCheck.areAllValuesSameWithAllMatch(map));
assertTrue(MapValuesCheck.areAllValuesSameWithReduce(map));
assertTrue(MapValuesCheck.areAllValuesSameWithLoop(map));
}

@Test
void givenMapWithAllSameValues_whenCheckingAllValuesSameWithSet_thenReturnTrue() {
Map<String, Integer> map = Map.of("English", 100, "Maths", 100, "Science", 100);

assertTrue(MapValuesCheck.areAllValuesSameWithSet(map));
}

@Test
void givenMapWithDifferentValues_whenCheckingAllValuesSameWithSet_thenReturnFalse() {
Map<String, Integer> map = Map.of("English", 1000, "Maths", 100, "Science", 100);

assertFalse(MapValuesCheck.areAllValuesSameWithSet(map));
}
}