sophiadw
sophiadw

Reputation: 567

combine boolean varible of all objects in an arraylist in Java

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

Answers (1)

khush
khush

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

Related Questions