From 6045b4291a618d4b00046aa9ae495f1ff325996b Mon Sep 17 00:00:00 2001 From: Jan Hauer Date: Thu, 8 Nov 2018 10:43:42 +0100 Subject: [PATCH] BAEL-2305 Adds array intersection operations --- .../array/operations/ArrayOperations.java | 14 ++++ .../operations/IntersectionUnitTest.java | 66 +++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 core-java-arrays/src/test/java/com/baeldung/array/operations/IntersectionUnitTest.java diff --git a/core-java-arrays/src/main/java/com/baeldung/array/operations/ArrayOperations.java b/core-java-arrays/src/main/java/com/baeldung/array/operations/ArrayOperations.java index 98155ed952..d8cc0afd61 100644 --- a/core-java-arrays/src/main/java/com/baeldung/array/operations/ArrayOperations.java +++ b/core-java-arrays/src/main/java/com/baeldung/array/operations/ArrayOperations.java @@ -4,11 +4,13 @@ import java.lang.reflect.Array; import java.util.Arrays; import java.util.HashSet; import java.util.LinkedHashSet; +import java.util.LinkedList; import java.util.Random; import java.util.Set; import java.util.function.Function; import java.util.function.IntPredicate; import java.util.function.Predicate; +import java.util.stream.Stream; import org.apache.commons.lang3.ArrayUtils; @@ -194,4 +196,16 @@ public class ArrayOperations { public static T getRandomFromObjectArray(T[] array) { return array[new Random().nextInt(array.length)]; } + + public static Integer[] intersectionSimple(final Integer[] a, final Integer[] b){ + return Stream.of(a).filter(Arrays.asList(b)::contains).toArray(Integer[]::new); + } + + public static Integer[] intersectionSet(final Integer[] a, final Integer[] b){ + return Stream.of(a).filter(Arrays.asList(b)::contains).distinct().toArray(Integer[]::new); + } + + public static Integer[] intersectionMultiSet(final Integer[] a, final Integer[] b){ + return Stream.of(a).filter(new LinkedList<>(Arrays.asList(b))::remove).toArray(Integer[]::new); + } } diff --git a/core-java-arrays/src/test/java/com/baeldung/array/operations/IntersectionUnitTest.java b/core-java-arrays/src/test/java/com/baeldung/array/operations/IntersectionUnitTest.java new file mode 100644 index 0000000000..3c61060ea8 --- /dev/null +++ b/core-java-arrays/src/test/java/com/baeldung/array/operations/IntersectionUnitTest.java @@ -0,0 +1,66 @@ +package com.baeldung.array.operations; + +import org.junit.jupiter.api.Test; + +import static com.baeldung.array.operations.ArrayOperations.intersectionMultiSet; +import static com.baeldung.array.operations.ArrayOperations.intersectionSet; +import static com.baeldung.array.operations.ArrayOperations.intersectionSimple; +import static org.assertj.core.api.Assertions.assertThat; + +class IntersectionUnitTest { + private static final Integer[] a = { 1, 3, 2 }; + private static final Integer[] b = { 4, 3, 2, 4, 2, 3, 4, 4, 3 }; + private static final Integer[] c = { 1, 3, 2, 3, 3, 2 }; + + @Test + void whenIntersectionSimpleIsUsed_thenCommonEntriesAreInTheResult() { + assertThat(intersectionSimple(a, b)).isEqualTo(new Integer[] { 3, 2 }); + assertThat(intersectionSimple(b, a)).isEqualTo(new Integer[] { 3, 2, 2, 3, 3 }); + } + + @Test + void whenIntersectionSimpleIsUsedWithAnArrayAndItself_thenTheResultIsTheIdentity() { + assertThat(intersectionSimple(b, b)).isEqualTo(b); + assertThat(intersectionSimple(a, a)).isEqualTo(a); + } + + @Test + void whenIntersectionSetIsUsed_thenCommonEntriesAreInTheResult() { + assertThat(intersectionSet(b, a)).isEqualTo(new Integer[] { 3, 2 }); + } + + @Test + void whenIntersectionSetIsUsed_thenTheNumberOfEntriesDoesNotChangeWithTheParameterOrder() { + assertThat(intersectionSet(a, b)).isEqualTo(new Integer[] { 3, 2 }); + assertThat(intersectionSet(b, a)).isEqualTo(new Integer[] { 3, 2 }); + } + + @Test + void whenIntersectionSetIsUsedWithAnArrayAndWithItself_andTheInputHasNoDuplicateEntries_ThenTheResultIsTheIdentity() { + assertThat(intersectionSet(a, a)).isEqualTo(a); + } + + @Test + void whenIntersectionSetIsUsedWithAnArrayAndWithItself_andTheInputHasDuplicateEntries_ThenTheResultIsNotTheIdentity() { + assertThat(intersectionSet(b, b)).isNotEqualTo(b); + } + + @Test + void whenMultiSetIsUsed_thenCommonEntriesAreInTheResult() { + assertThat(intersectionMultiSet(b, a)).isEqualTo(new Integer[] { 3, 2 }); + } + + @Test + void whenIntersectionMultiSetIsUsed_thenTheNumberOfEntriesDoesNotChangeWithTheParameterOrder() { + assertThat(intersectionMultiSet(a, b)).isEqualTo(new Integer[] { 3, 2 }); + assertThat(intersectionMultiSet(b, a)).isEqualTo(new Integer[] { 3, 2 }); + assertThat(intersectionMultiSet(b, c)).isEqualTo(new Integer[] { 3, 2, 2, 3, 3 }); + assertThat(intersectionMultiSet(c, b)).isEqualTo(new Integer[] { 3, 2, 3, 3, 2 }); + } + + @Test + void whenIntersectionMultiSetIsUsedWithAnArrayAndWithItself_ThenTheResultIsTheIdentity() { + assertThat(intersectionMultiSet(b, b)).isEqualTo(b); + assertThat(intersectionMultiSet(a, a)).isEqualTo(a); + } +} \ No newline at end of file