Issue
Find duplicates in two lists List<int[]> with streams
I have a list of int arrays. I need to find duplicates of these arrays using 2
lists of int arrays.
I did try to implement it, but I'm getting an empty array.
keys = [[1,1,0], [0,0,1], [1,2,1], [1,3,1], [1,3,2]];
phaseKey = [[1,3,2], [1,2,1], [0,0,2], [1,2,3], [1,0,3]];
desired result: [[[1,3,2]], [1,2,1]];
My code:
Stream.concat(Stream.of(keys.stream().flatMapToInt(Arrays::stream)),
Stream.of(phaseKey.stream().flatMapToInt(Arrays::stream)))
.collect(Collectors.groupingBy(
Function.identity(),
Collectors.counting()))
.entrySet()
.stream()
.filter(m -> m.getValue() > 1)
.map(Map.Entry::getKey)
.toArray();
Solution
Given your two lists:
List<int[]> keys = // ....
List<int[]> phaseKey = //...
You just need to filter to find common arrays in both lists:
List<int[]> duplicates = keys.stream()
.filter(k -> phaseKey.stream().anyMatch(p -> Arrays.equals(p,k)))
.collect(Collectors.toList());
Answered By - Eritrean
Answer Checked By - Terry (JavaFixing Volunteer)