1

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());

1 Answer 1

2

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))

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.