From 0c229c433ff4ba7e0bab1dfe3bad37859915188c Mon Sep 17 00:00:00 2001 From: Kai Yuan Date: Mon, 28 Mar 2022 03:16:58 +0200 Subject: [PATCH] add SetDiff for BAEL-5464 article (#11969) * add SetDiff for BAEL-5464 article * rename the method --- .../main/java/com/baeldung/set/SetDiff.java | 28 +++++++++ .../com/baeldung/set/SetDiffUnitTest.java | 61 +++++++++++++++++++ 2 files changed, 89 insertions(+) create mode 100644 core-java-modules/core-java-collections-set/src/main/java/com/baeldung/set/SetDiff.java create mode 100644 core-java-modules/core-java-collections-set/src/test/java/com/baeldung/set/SetDiffUnitTest.java diff --git a/core-java-modules/core-java-collections-set/src/main/java/com/baeldung/set/SetDiff.java b/core-java-modules/core-java-collections-set/src/main/java/com/baeldung/set/SetDiff.java new file mode 100644 index 0000000000..4a62c99efd --- /dev/null +++ b/core-java-modules/core-java-collections-set/src/main/java/com/baeldung/set/SetDiff.java @@ -0,0 +1,28 @@ +package com.baeldung.set; + +import java.util.HashMap; +import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; + +public class SetDiff { + + public static Set findSymmetricDiff(Set set1, Set set2) { + Map map = new HashMap<>(); + set1.forEach(e -> putKey(map, e)); + set2.forEach(e -> putKey(map, e)); + return map.entrySet().stream() + .filter(e -> e.getValue() == 1) + .map(Map.Entry::getKey) + .collect(Collectors.toSet()); + } + + private static void putKey(Map map, T key) { + if (map.containsKey(key)) { + map.replace(key, Integer.MAX_VALUE); + } else { + map.put(key, 1); + } + } + +} diff --git a/core-java-modules/core-java-collections-set/src/test/java/com/baeldung/set/SetDiffUnitTest.java b/core-java-modules/core-java-collections-set/src/test/java/com/baeldung/set/SetDiffUnitTest.java new file mode 100644 index 0000000000..c217950a35 --- /dev/null +++ b/core-java-modules/core-java-collections-set/src/test/java/com/baeldung/set/SetDiffUnitTest.java @@ -0,0 +1,61 @@ +package com.baeldung.set; + +import com.google.common.collect.Sets; +import org.apache.commons.collections4.CollectionUtils; +import org.apache.commons.collections4.SetUtils; +import org.junit.Test; + +import java.util.HashSet; +import java.util.Set; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import static org.assertj.core.api.Assertions.assertThat; + +public class SetDiffUnitTest { + private final static Set immutableSet1 = Set.of("Kotlin", "Java", "Rust", "Python", "C++"); + private final static Set immutableSet2 = Set.of("Kotlin", "Java", "Rust", "Ruby", "C#"); + private final static Set expectedOnlyInSet1 = Set.of("Python", "C++"); + private final static Set expectedDiff = Set.of("Python", "C++", "Ruby", "C#"); + + @Test + public void givenAnotherSet_whenRemoveAll_shouldGetDiff() { + Set set1 = Stream.of("Kotlin", "Java", "Rust", "Python", "C++").collect(Collectors.toSet()); + Set set2 = Stream.of("Kotlin", "Java", "Rust", "Ruby", "C#").collect(Collectors.toSet()); + Set expectedOnlyInSet1 = Set.of("Python", "C++"); + set1.removeAll(set2); + assertThat(set1).isEqualTo(expectedOnlyInSet1); + } + + @Test + public void givenAnotherSet_whenUsingStreamFilter_shouldGet() { + Set actualOnlyInSet1 = immutableSet1.stream().filter(e -> !immutableSet2.contains(e)).collect(Collectors.toSet()); + assertThat(actualOnlyInSet1).isEqualTo(expectedOnlyInSet1); + } + + @Test + public void givenAnotherSet_whenCallingGuavaMethod_shouldGetDiff() { + Set actualOnlyInSet1 = Sets.difference(immutableSet1, immutableSet2); + assertThat(actualOnlyInSet1).isEqualTo(expectedOnlyInSet1); + } + + @Test + public void givenAnotherSet_whenCallingCommonsMethod_shouldGetDiff() { + Set actualOnlyInSet1 = new HashSet<>(CollectionUtils.removeAll(immutableSet1, immutableSet2)); + assertThat(actualOnlyInSet1).isEqualTo(expectedOnlyInSet1); + } + + + @Test + public void givenTwoSets_whenCallingFindDisjunction_shouldGetDisjunction() { + Set actualDiff = SetDiff.findSymmetricDiff(immutableSet1, immutableSet2); + assertThat(actualDiff).isEqualTo(expectedDiff); + } + + @Test + public void givenTwoSets_whenCallingCommonsMethod_shouldGetDisjunction() { + Set actualDiff = SetUtils.disjunction(immutableSet1, immutableSet2); + assertThat(actualDiff).isEqualTo(expectedDiff); + } + +}