Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retainAll of List of Lists using stream reduce

I faced following problem. I have a list of lists which i simply want to retainAll. I'm trying to do with streams

private List<List<Long>> ids = new ArrayList<List<Long>>();

// some ids.add(otherLists);  

List<Long> reduce = ids.stream().reduce(ids.get(0), (a, b) -> a.addAll(b));

unfortunately I got the error

Error:(72, 67) java: incompatible types: bad return type in lambda expression
    boolean cannot be converted to java.util.List<java.lang.Long> 
like image 460
pezetem Avatar asked Apr 23 '15 14:04

pezetem


People also ask

Does Stream filter modify the original List?

stream(). filter(i -> i >= 3); does not change original list. All stream operations are non-interfering (none of them modify the data source), as long as the parameters that you give to them are non-interfering too.


2 Answers

If you want to reduce (I think you mean flatten by that) the list of lists, you should do it like this:

import static java.util.stream.Collectors.toList

...

List<Long> reduce = ids.stream().flatMap(List::stream).collect(toList());

Using reduce, the first value should be the identity value which is not the case in your implementation, and your solution will produce unexpected results when running the stream in parallel (because addAll modifies the list in place, and in this case the identity value will be the same list for partial results).

You'd need to copy the content of the partial result list, and add the other list in it to make it working when the pipeline is run in parallel:

List<Long> reduce = ids.parallelStream().reduce(new ArrayList<>(), (a, b) -> {
    List<Long> list = new ArrayList<Long>(a);
    list.addAll(b);
    return list;
 });
like image 152
Alexis C. Avatar answered Oct 09 '22 22:10

Alexis C.


addAll returns a boolean, not the union of the two lists. You want

List<Long> reduce = ids.stream().reduce(ids.get(0), (a, b) -> {
    a.addAll(b);
    return a;
});
like image 30
lbalazscs Avatar answered Oct 10 '22 00:10

lbalazscs