I have an ArrayList<Person> persons
. I want to check if some person in persons fulfills a condition. Like: person.isFemale()
Instead of looping the list, is there any nicer way to perform this? Some mapping or lambda way maybe?
Edit:
Hello and thanks for the replies!
I think I asked the wrong question.. I want to check if any object in the list differ from any other: boolean different = (if isMale() && isFemale()) somewhere in the list.
I would recommend Guava (formally Google Collections), specifically Iterables.any to test if a single instance matches a condition or Iterables.all to test if all instances match a condition. You can set your predicate to either match some logical expression or test all elements are equal to the head of the list.
This isn't doing anything fancy under the hood, but it does at least get you into the habit of writing code in the functional style.
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