Intersection of two List<int[]> types by java streams -


list<int[]> biglist = new arraylist<int[]>(); list<int[]> smalllist = new arraylist<int[]>(); 

i need generate list of type int[] common arrays form both lists.(values should equal , not using contains())

how efficiently in java streams??

if has stream solution, here’s one:

list<int[]> intersection=biglist.stream().map(intbuffer::wrap)     .filter(b->smalllist.stream().map(intbuffer::wrap).anymatch(b::equals))     .map(intbuffer::array)     .collect(collectors.tolist()); 

but isn’t efficient performing biglist.size()×smalllist.size() operations. instead of doing on-the-fly, resorting intermediate set storage recommended:

set<intbuffer> bigset=biglist.stream().map(intbuffer::wrap).collect(collectors.toset()); list<int[]> intersection=smalllist.stream().map(intbuffer::wrap)      .filter(bigset::contains).map(intbuffer::array).collect(collectors.tolist()); 

note shouldn’t use list set operations. semantics of source , result ordering , how handle duplicates within source lists unspecified.


Comments