Reputation: 567
I have an arraylist of self defined objects (e.g., person). each obj has a boolean variable v1 (e.g., isMale). I want to check if v1 of all objs in this arraylist is true on v1, if so, return true, if any obj has v1 == false, return false. I can write a loop to achieve this. But I am wondering if there is faster way to do so. Thanks!
Upvotes: 1
Views: 519
Reputation: 2799
Use Stream API's terminal function anyMatch to improve performance
List<YourClass> list = Arrays.asList(// list of objects)
boolean result = list.stream().anyMatch((obj) -> !obj.isMale); // your required value
Upvotes: 4