Reputation: 25
Here is the code for comparing one value in one list with another list.How to rewrite the code if I need a list of values, by comparing first list with another value in sublist having multiple objects of another list.
Suppose here hitsList contain another sublist ,and need to compare one value in item object with another value in list of hit object.
List<Item> itemList3 = Item.parallelStream()
.filter(item -> hits.stream()
.anyMatch(hit ->
item.getProductRecId().equals(hit.getCrtRecID())
))
.collect(Collectors.toList());
Upvotes: 1
Views: 460
Reputation: 7279
Try this:
Set<Hit> ctrRecIDs = hits.stream()
.map(Hit::getCrtRecID)
.collect(Collectors.toSet());
List<Item> filteredItems = items.stream()
.filter(it -> ctrRecIDs.contains(it.getProductRecId())
.collect(Collectors.toList());
The time complexity of the solution above is linear (O(n)
), while your initial one is squared (O(n^2)
)
Upvotes: 2