I do not believe that there is any part of the Java Standard Libraries that provides exactly this functionality.
In some languages, they are provided as functions called anyOf (for or ) or allOf (for and ). You may be lucky to look for Java libraries that implement these functions.
Note. The code you are here can be optimized quite a bit. Note: if you compute AND from many booleans, once you find false , you can stop and say that the answer is false . Similarly, if you calculate the OR of booleans, you can stop as soon as you find true , since the answer will be true . This is essentially a generalization of a short circuit to lists of objects and the behavior of some versions of and and or in languages ββlike Scheme. This gives the following:
public class ModifiedBooleans3 { private ModifiedBooleans3(){} public static boolean and(Iterable<Boolean> booleans) { for (Boolean boolRef : booleans) if (!boolRef) return false; return true; } public static boolean or(Iterable<Boolean> booleans) { for (Boolean boolRef : booleans) if (boolRef) return true; return false; } }
Hope this helps!
source share