0

I am trying to compare one arrayList object elements contains another arraylist with below code but this is not working as i expected can some one help me please

arrayList1=[{productSku:"123"},{productSku:"1234"}]
arrayList2=[{productSku:"123"},{productSku:"1000"}]

My scenario is if arraylist1 elements not matching with any of the element in arraylist2 then we should throw exception

arrayList2.stream().filter(type -> "EQUIPMENT".equals(type.getItemType()))
                        .forEach(action -> {                             
            arrayList1.forEach(action1 -> {
                                if (!action1.getProductSku().equalsIgnoreCase(action.getProductSKU())) {
                                    // Throw exception
                                }
                            });
                        });
3
  • 1
    Define not matching. What is the epected result of your example? Why? Commented Apr 23, 2021 at 12:14
  • 1
    Just to clarify: you throw the exception when NO element from arraylist1 matches any element of arraylist? or do you even throw the exception when only one element from arraylist1 can't be found in arraylist? Commented Apr 23, 2021 at 12:14
  • 1
    i want to throw exception when NO element from arraylist1 matches any element of arraylist2 Commented Apr 23, 2021 at 12:16

2 Answers 2

2

While Deadpool's answer is great, I'd be more inclined to avoid using Optional here, as there is no use for the actual value to be used.

Also, creating a Stream for each element traversing the Stream is not a good idea in terms of performance.

Set<String> products = arrayList1.stream()
                                 .map(Action::getProductSku)
                                 .map(String::toUpperCase)
                                 .collect(toSet());

boolean shouldThrowAnException = arrayList2.stream()
                                           .filter(type -> "EQUIPMENT".equals(type.getItemType()))
                                           .map(Action::getProductSku)
                                           .map(String::toUpperCase)
                                           .noneMatch(products::contains);

if (shouldThrowAnException) {
    // throw exception
}
Sign up to request clarification or add additional context in comments.

Comments

1

You can stream the arrayList2 and try finding at least one matching element in arrayList1 using findFirst or else throw exception

arrayList2.stream()
     .filter(type -> "EQUIPMENT".equals(type.getItemType()))
     .filter(list2-> arrayList1.stream()
           .anyMatch(list1->list1.getProductSku().equals(list2.getProductSku())))
     .findFirst()
     .orElseThrow(//some exception)

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.