5

I have two functions which check if all elements of an array or list are true. I'm having trouble combining the two. How can I make the functions into one generic Java function.

public static boolean allTrue(boolean[] booleans) {
    if (booleans == null) {
        return false;
    }

    for (boolean bool : booleans) {
        if (!bool) {
            return false;
        }
    }
    return true;
}

public static boolean allTrue(List<Boolean> booleans) {
    if (booleans == null) {
        return false;
    }

    for (boolean bool : booleans) {
        if (!bool) {
            return false;
        }
    }
    return true;
}
Kwoppy
  • 289
  • 3
  • 10

4 Answers4

4

If you're using Guava, you can wrap the boolean array in Booleans.asList() and pass it as a list:

public static boolean allTrue(boolean[] booleans) {
    return booleans != null && allTrue(Booleans.asList(booleans));
}
shmosel
  • 49,289
  • 6
  • 73
  • 138
2

As per https://stackoverflow.com/a/5606435/2310289

You could just accept an Object

public static boolean allTrue(Object booleans) {

and then check for instanceof boolean[] or instanceof List<Boolean> and then perform different code within the method.

Again, not really an improvement, but a bit closer to code unification

Community
  • 1
  • 1
Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
1

I think the answer given by @Joel was a good one, except for the issue pointed out in the comment. If we just convert boolean[] to Boolean[], we can try the following:

public static boolean allTrue(List<Boolean> booleans) {
    if (booleans == null) {
        return false;
    }

    for (boolean bool : booleans) {
        if (!bool) {
            return false;
        }
    }
    return true;
}

public static boolean allTrue(boolean[] booleans) {
    Boolean[] newArray = new Boolean[booleans.length];
    int i = 0;
    for (boolean value : booleans) {
        newArray[i++] = Boolean.valueOf(value);
    }

    return Arrays.asList(newArray);
}
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
1

The common ancestor for List<Boolean> and boolean[] is Object, so unless you are okay with allTrue(Object booleans), you cannot do it with one method.

If you change your method signature to allTrue(Iterable<Boolean> booleans), all you have to do is create a special Iterator<Boolean> to traverse the boolean array.

import java.util.Iterator;
import java.util.NoSuchElementException;

public class BooleanAllTrue {
    public static boolean allTrue(Iterable<Boolean> booleans) {
        if (booleans == null) return false;

        for (Boolean bool : booleans) {
            if (!bool) return false;
        }

        return true;
    }

    public static Iterable<Boolean> asIterable(final boolean[] booleens) {
        return new Iterable<Boolean>() {
            public Iterator<Boolean> iterator() {
                final boolean[] booleans = booleens;
                return new Iterator<Boolean>() {
                    private int i = 0;

                    public boolean hasNext() {
                        return i < booleans.length;
                    }

                    public Boolean next() {
                        if (!hasNext()) throw new NoSuchElementException();
                        return booleans[i++];
                    }

                    public void remove() {throw new UnsupportedOperationException("remove");}
                };
            }
        };
    }

    public static void main(String [] args) {
        System.out.println(allTrue(asIterable(new boolean[]{true, true})));
        System.out.println(allTrue(asIterable(new boolean[]{true, false})));
        try {
            asIterable(new boolean[0]).iterator().next();
        } catch (NoSuchElementException e) {
            // expected
        }
    }
}

And finally the allTrue(boolean[] booleans) method.

public static boolean allTrue(boolean[] booleans) {
    return allTrue(asIterable(booleans));
}
xiaofeng.li
  • 8,237
  • 2
  • 23
  • 30
  • 1
    *The common ancestor for `List` and `boolean[]` is `Object`*. If you're counting interfaces, they both implement `Serializable`. Not that that's relevant to your point. – shmosel Feb 10 '17 at 03:22