What is the most elegant way to check if all values in a boolean array are true?
JavaArraysBooleanJava Problem Overview
I have a boolean array in java:
boolean[] myArray = new boolean[10];
What's the most elegant way to check if all the values are true?
Java Solutions
Solution 1 - Java
public static boolean areAllTrue(boolean[] array)
{
for(boolean b : array) if(!b) return false;
return true;
}
Solution 2 - Java
Arrays.asList(myArray).contains(false)
Solution 3 - Java
In Java 8, you could do:
boolean isAllTrue = Arrays.asList(myArray).stream().allMatch(val -> val == true);
Or even shorter:
boolean isAllTrue = Arrays.stream(myArray).allMatch(Boolean::valueOf);
Note:
You need Boolean[]
for this solution to work. Because you can't have a primitives List.
Solution 4 - Java
It depends how many times you're going to want to find this information, if more than once:
Set<Boolean> flags = new HashSet<Boolean>(myArray);
flags.contains(false);
Otherwise a short circuited loop:
for (i = 0; i < myArray.length; i++) {
if (!myArray[i]) return false;
}
return true;
Solution 5 - Java
I can't believe there's no BitSet
solution.
A BitSet
is an abstraction over a set of bits so we don't have to use boolean[]
for more advanced interactions anymore, because it already contains most of the needed methods. It's also pretty fast in batch operations since it internally uses long
values to store the bits and doesn't therefore check every bit separately like we do with boolean[]
.
BitSet myBitSet = new BitSet(10);
// fills the bitset with ten true values
myBitSet.set(0, 10);
For your particular case, I'd use cardinality()
:
if (myBitSet.cardinality() == myBitSet.size()) {
// do something, there are no false bits in the bitset
}
Another alternative is using Guava:
return Booleans.contains(myArray, true);
Solution 6 - Java
That line should be sufficient:
BooleanUtils.and(boolean... array)
but to calm the link-only purists:
> Performs an and on a set of booleans.
Solution 7 - Java
In Java 8+, you can create an IntStream
in the range of 0
to myArray.length
and check that all values are true
in the corresponding (primitive) array with something like,
return IntStream.range(0, myArray.length).allMatch(i -> myArray[i]);
Solution 8 - Java
This is probably not faster, and definitely not very readable. So, for the sake of colorful solutions...
int i = array.length()-1;
for(; i > -1 && array[i]; i--);
return i==-1
Solution 9 - Java
boolean alltrue = true;
for(int i = 0; alltrue && i<booleanArray.length(); i++)
alltrue &= booleanArray[i];
I think this looks ok and behaves well...
Solution 10 - Java
Simply convert the array to a String
using Arrays.toString
and test if it contains false
.
Demo:
import java.util.Arrays;
public class Main {
public static void main(String args[]) {
// Test
System.out.println(areAllValuesTrue(new boolean[] { true, true, true, true }));
System.out.println(areAllValuesTrue(new boolean[] { true, false, false, true }));
System.out.println(areAllValuesTrue(new boolean[] { false, false, false, false }));
}
static boolean areAllValuesTrue(boolean[] arr) {
return !Arrays.toString(arr).contains("false");
}
}
Output:
true
false
false
Solution 11 - Java
You can check all value items are true or false by compare your array with the other boolean array via Arrays.equal
method like below example :
private boolean isCheckedAnswer(List<Answer> array) {
boolean[] isSelectedChecks = new boolean[array.size()];
for (int i = 0; i < array.size(); i++) {
isSelectedChecks[i] = array.get(i).isChecked();
}
boolean[] isAllFalse = new boolean[array.size()];
for (int i = 0; i < array.size(); i++) {
isAllFalse[i] = false;
}
return !Arrays.equals(isSelectedChecks, isAllFalse);
}
Solution 12 - Java
Kotlin: if one elemnt is false then not all are selected
return list.filter { isGranted -> isGranted.not() }.isNotEmpty()
Solution 13 - Java
OK. This is the "most elegant" solution I could come up with on the fly:
boolean allTrue = !Arrays.toString(myArray).contains("f");
Hope that helps!