I'm trying to write the following condition:
if(javaList.contains("aaa")||javaList.contains("abc")||javaList.contains("abc")) {
//do something
}
How can I do it in a better way?
It's usually more efficient to run contains
on a Set
than on a List
. Therefore I suggest you create a Set
of the elements you want to test, and then stream over the List
to see if any of its elements matches:
How about:
if (javaList.stream().anyMatch(e -> Set.of("aaa","abc","def").contains(e)))
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