Maybe something like this:
public static void main(String[] args) {
Integer a[] = {1, 2, 3, 4};
Integer b[] = {9, 8, 2, 3};
Stream<Integer> as = Arrays.stream(a).distinct();
Stream<Integer> bs = Arrays.stream(b).distinct();
List<Integer> collect = Stream.concat(as, bs)
.collect(Collectors.groupingBy(Function.identity()))
.entrySet()
.stream()
.filter(e -> e.getValue().size() > 1)
.map(e -> e.getKey())
.collect(Collectors.toList());
System.out.println(collect);
}
- we merge two array into one stream
groupBy
is counting by value
- then we filter lists longer than 1, that lists contains duplicates
map
to key to extract value of duplicated entry
- print it.
edit: added distinct to initial streams.