Skip to content

Commit 6045b42

Browse files
Jan HauerJan Hauer
authored andcommitted
BAEL-2305 Adds array intersection operations
1 parent 4d30ff3 commit 6045b42

2 files changed

Lines changed: 80 additions & 0 deletions

File tree

core-java-arrays/src/main/java/com/baeldung/array/operations/ArrayOperations.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44
import java.util.Arrays;
55
import java.util.HashSet;
66
import java.util.LinkedHashSet;
7+
import java.util.LinkedList;
78
import java.util.Random;
89
import java.util.Set;
910
import java.util.function.Function;
1011
import java.util.function.IntPredicate;
1112
import java.util.function.Predicate;
13+
import java.util.stream.Stream;
1214

1315
import org.apache.commons.lang3.ArrayUtils;
1416

@@ -194,4 +196,16 @@ public static int getRandomFromIntArray(int[] array) {
194196
public static <T> T getRandomFromObjectArray(T[] array) {
195197
return array[new Random().nextInt(array.length)];
196198
}
199+
200+
public static Integer[] intersectionSimple(final Integer[] a, final Integer[] b){
201+
return Stream.of(a).filter(Arrays.asList(b)::contains).toArray(Integer[]::new);
202+
}
203+
204+
public static Integer[] intersectionSet(final Integer[] a, final Integer[] b){
205+
return Stream.of(a).filter(Arrays.asList(b)::contains).distinct().toArray(Integer[]::new);
206+
}
207+
208+
public static Integer[] intersectionMultiSet(final Integer[] a, final Integer[] b){
209+
return Stream.of(a).filter(new LinkedList<>(Arrays.asList(b))::remove).toArray(Integer[]::new);
210+
}
197211
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.baeldung.array.operations;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import static com.baeldung.array.operations.ArrayOperations.intersectionMultiSet;
6+
import static com.baeldung.array.operations.ArrayOperations.intersectionSet;
7+
import static com.baeldung.array.operations.ArrayOperations.intersectionSimple;
8+
import static org.assertj.core.api.Assertions.assertThat;
9+
10+
class IntersectionUnitTest {
11+
private static final Integer[] a = { 1, 3, 2 };
12+
private static final Integer[] b = { 4, 3, 2, 4, 2, 3, 4, 4, 3 };
13+
private static final Integer[] c = { 1, 3, 2, 3, 3, 2 };
14+
15+
@Test
16+
void whenIntersectionSimpleIsUsed_thenCommonEntriesAreInTheResult() {
17+
assertThat(intersectionSimple(a, b)).isEqualTo(new Integer[] { 3, 2 });
18+
assertThat(intersectionSimple(b, a)).isEqualTo(new Integer[] { 3, 2, 2, 3, 3 });
19+
}
20+
21+
@Test
22+
void whenIntersectionSimpleIsUsedWithAnArrayAndItself_thenTheResultIsTheIdentity() {
23+
assertThat(intersectionSimple(b, b)).isEqualTo(b);
24+
assertThat(intersectionSimple(a, a)).isEqualTo(a);
25+
}
26+
27+
@Test
28+
void whenIntersectionSetIsUsed_thenCommonEntriesAreInTheResult() {
29+
assertThat(intersectionSet(b, a)).isEqualTo(new Integer[] { 3, 2 });
30+
}
31+
32+
@Test
33+
void whenIntersectionSetIsUsed_thenTheNumberOfEntriesDoesNotChangeWithTheParameterOrder() {
34+
assertThat(intersectionSet(a, b)).isEqualTo(new Integer[] { 3, 2 });
35+
assertThat(intersectionSet(b, a)).isEqualTo(new Integer[] { 3, 2 });
36+
}
37+
38+
@Test
39+
void whenIntersectionSetIsUsedWithAnArrayAndWithItself_andTheInputHasNoDuplicateEntries_ThenTheResultIsTheIdentity() {
40+
assertThat(intersectionSet(a, a)).isEqualTo(a);
41+
}
42+
43+
@Test
44+
void whenIntersectionSetIsUsedWithAnArrayAndWithItself_andTheInputHasDuplicateEntries_ThenTheResultIsNotTheIdentity() {
45+
assertThat(intersectionSet(b, b)).isNotEqualTo(b);
46+
}
47+
48+
@Test
49+
void whenMultiSetIsUsed_thenCommonEntriesAreInTheResult() {
50+
assertThat(intersectionMultiSet(b, a)).isEqualTo(new Integer[] { 3, 2 });
51+
}
52+
53+
@Test
54+
void whenIntersectionMultiSetIsUsed_thenTheNumberOfEntriesDoesNotChangeWithTheParameterOrder() {
55+
assertThat(intersectionMultiSet(a, b)).isEqualTo(new Integer[] { 3, 2 });
56+
assertThat(intersectionMultiSet(b, a)).isEqualTo(new Integer[] { 3, 2 });
57+
assertThat(intersectionMultiSet(b, c)).isEqualTo(new Integer[] { 3, 2, 2, 3, 3 });
58+
assertThat(intersectionMultiSet(c, b)).isEqualTo(new Integer[] { 3, 2, 3, 3, 2 });
59+
}
60+
61+
@Test
62+
void whenIntersectionMultiSetIsUsedWithAnArrayAndWithItself_ThenTheResultIsTheIdentity() {
63+
assertThat(intersectionMultiSet(b, b)).isEqualTo(b);
64+
assertThat(intersectionMultiSet(a, a)).isEqualTo(a);
65+
}
66+
}

0 commit comments

Comments
 (0)