Intersection between nested lists java 8 streams

  • A+
Category:Languages

I have a nested list of Long. for example:

List<List<Long>> ids = [[1,2,3],[1,2,3,4],[2,3]]; 

Is there a way using streams to create a new list of items that are present in all the lists:

List<Long> result = [2,3]; 

 


There is quite concise solution without stream:

List<Long> result = new ArrayList<>(ids.get(0)); ids.forEach(result::retainAll);  System.out.println(result); 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: