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;
}
8
  • Are you using Guava by any chance? Commented Feb 10, 2017 at 2:40
  • see stackoverflow.com/a/5606435/2310289 Commented Feb 10, 2017 at 2:49
  • @shmosel No Guava Commented Feb 10, 2017 at 2:55
  • How about Java 8? Commented Feb 10, 2017 at 2:55
  • 1
    And if you're treating null as an empty array, it should return true instead. Commented Feb 10, 2017 at 3:16

4 Answers 4

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));
}
Sign up to request clarification or add additional context in comments.

9 Comments

Under the hood, this probably is not more efficient than the answer I gave.
I don't see any indication OP was concerned about efficiency.
Both answers are less efficient than the code posted in the question.
@Winter There is no requirement for efficiency improvement
@Winter I'm pretty sure OP didn't mean generic in the technical sense. He just wants to unify the logic.
|
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

Comments

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

1 Comment

How is that not an improvement? The logic of allTrue now only needs to be changed in one location if necessary.
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));
}

1 Comment

The common ancestor for List<Boolean> and boolean[] is Object. If you're counting interfaces, they both implement Serializable. Not that that's relevant to your point.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.