In my opinions, your example is a particular case of join operation, because your result set doesn't take any column of SearchResult. For most people going into this question like me, the general join operation with Java Stream is what they want. And here is my solution :
- step 1, flatMap with judging whether the result should be null or not
- step 2, filter the stream in step 1 by nonNull
It works fine, although it's not so elegant, which seems like u implement a join operation yourself--
The pseudocode may like :
//init the two list
List l1 = ...;
List l2 = ...;
//join operation
res = l1.stream().flatMap(
_item1 -> l2.stream().map(_item2 -> (_item1.join_key == _item2.join_key)?
([take the needed columns in item1 and item2]) : null
)
).filter(Object::nonNull);