Im trying to use Java 8 streams to combine lists. How can I get a \"symmetric difference list\" (all object that only exist in one list) from two existing lists. I know how
Something like this may work:
Stream.concat(bigCarList.stream(), smallCarList.stream())
.collect(groupingBy(Function.identity(), counting()))
.entrySet().stream()
.filter(e -> e.getValue().equals(1L))
.map(Map.Entry::getKey)
.collect(toList());
Here we first collect all the cars to the Map
where value is the number of such cars encountered. After that, we filter
this Map
leaving only cars that are encountered exactly once, drop the counts and collect to the final List
.