How to replace this code with using Java Stream Api?
for (Book book : Books.getAllBooks()) {
for (SearchResult searchResult : searchResultList) {
if (searchResult.getTableName().replace("attr_", "") .equals(book.getTableName())) {
bookList.add(book);
}
}
}
List<Book> bookList = Books.getAllBooks().stream()
.filter(e -> searchResultList.stream()
.anyMatch(f -> e.getTableName().equals(f.getTableName().replace("attr_", ""))))
.collect(Collectors.toList());
I came from C# and missed that feature in Java 8 API, so I wrote my own. With streamjoin you can write
Stream<Book> books =
join(Books.getAllBooks().stream())
.withKey(Book::getTableName)
.on(searchResultList.stream())
.withKey(SearchResult::getTableName)
.combine((book, searchResult) -> book)
.asStream()
Not exactly what you asked for, but here's another trick if you're using Guava:
List<Book> bookList = new ArrayList<>(Books.getAllBooks());
Lists.transform(bookList, Book::getTableName)
.retainAll(Lists.transform(searchResultList, r -> r.getTableName().replace("attr_", ""));
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 :
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With