donturner
donturner

Reputation: 19146

What is the most elegant way to check if all values in a boolean array are true?

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?

Upvotes: 91

Views: 191880

Answers (12)

Javier
Javier

Reputation: 1685

Kotlin: if one elemnt is false then not all are selected

return list.filter { isGranted -> isGranted.not() }.isNotEmpty()

Upvotes: -1

Petr Janeček
Petr Janeček

Reputation: 38424

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);

Upvotes: 13

Gerold Broser
Gerold Broser

Reputation: 14762

That line should be sufficient:

BooleanUtils.and(boolean... array)

but to calm the link-only purists:

Performs an and on a set of booleans.

Upvotes: 11

Elliott Frisch
Elliott Frisch

Reputation: 201439

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]);

Upvotes: 10

Huy Tower
Huy Tower

Reputation: 7966

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);
}

Upvotes: 0

Kapil Sharma
Kapil Sharma

Reputation: 1432

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.

Upvotes: 41

Patrick
Patrick

Reputation: 881

boolean alltrue = true;
for(int i = 0; alltrue && i<booleanArray.length(); i++)
   alltrue &= booleanArray[i];

I think this looks ok and behaves well...

Upvotes: 2

Rich O&#39;Kelly
Rich O&#39;Kelly

Reputation: 41757

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;

Upvotes: 18

Eng.Fouad
Eng.Fouad

Reputation: 117589

public static boolean areAllTrue(boolean[] array)
{
    for(boolean b : array) if(!b) return false;
    return true;
}

Upvotes: 99

acorello
acorello

Reputation: 4653

Arrays.asList(myArray).contains(false)

Upvotes: 99

Miquel
Miquel

Reputation: 15675

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

Upvotes: 2

whirlwin
whirlwin

Reputation: 16521

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!

Upvotes: -5

Related Questions