Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to AND all booleans in a list using lambdas?

Tags:

java

I have a list of objects from which I extract the booleans and want to apply the AND operation on them. Is there a better (or more concise) way to do this?

final boolean[] result = {true};
someList.stream().map(o -> o.getBooleanMember()).forEach(b -> result = result && b);
like image 770
Darth Ninja Avatar asked Feb 06 '23 22:02

Darth Ninja


1 Answers

You can use reduce :

boolean result = someList.stream().map(o -> o.getBooleanMember()).reduce(true,(a,b)->a&&b);
like image 153
Eran Avatar answered Feb 23 '23 12:02

Eran