19

How do I code in Java the following python lines?

a = [True, False]
any (a)
all (a)

inb4 "What have you tried?"

The sledge-hammer way would be writing my own all and any methods (and obviously a class to host them):

public boolean any (boolean [] items)
{
    for (boolean item: items)
        if (item) return true;
    return false;
}

//other way round for all

But I don't plan on re-inventing the wheel and there must be a neat way to do this...

3 Answers 3

13

any() is the same thing as Collection#contains(), which is part of the standard library, and is in fact an instance method of all Collection implementations.

There is no built-in all(), however. The closest you'll get, aside from your "sledgehammer" approach, is Google Guava's Iterables#all().

6
  • Thank you. So no avail in the stdlibs? Commented Sep 22, 2013 at 4:19
  • 7
    My heart fills with sadness and my eyes fill with tears. Thank you. Waiting on CD for accept. Commented Sep 22, 2013 at 4:21
  • Guava will make Java much nicer for you overall (and moreso when you can just lambdas, as in Java 8). Get acquainted.
    – Matt Ball
    Commented Sep 22, 2013 at 4:23
  • Even if there is no bulit-in all(), you can easily get it using De Morgan's laws. That is, all(a) = !any(!a).
    – Jeff Irwin
    Commented Sep 12, 2015 at 22:51
  • 5
    any() is not the same thing as Collection#contains(). any() can be given any iterable and it'll return True as soon as it encounters a true value in the iterable. The important part here is that in Python you generally use a generator expression to provide the iterable; for example: any(path.startswith(prefix) for prefix in collection_of_prefixes). This makes any() useful for arbitrary tests. Collection#contains() only supports equals tests. The Java 8 streaming API anyMatch() test is a good equivalent. Commented Jun 22, 2017 at 13:20
11

In Java 7 and earlier, there is nothing in the standard libraries for doing that.

In Java 8, you should be able to use Stream.allMatch(...) or Stream.anyMatch(...) for this kind of thing, though I'm not sure that this would be justifiable from a performance perspective. (For a start, you would need to use Boolean instead of boolean ...)

1
  • Thank you for your answer. Let's see when Java8 hits dalvik. Commented Sep 22, 2013 at 4:28
10

An example for Java 8 streaming API would be:

Boolean[] items = ...;
List<Boolean> itemsList = Arrays.asList(items);
if (itemsList.stream().allMatch(e -> e)) {
    // all
}
if (itemsList.stream().anyMatch(e -> e)) {
    // any
}

A solution with the third party library hamcrest:

import static org.hamcrest.Matchers.everyItem;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.is;

if (everyItem(is(true)).matches(itemsList)) {
    // all
}
if (hasItem(is(true)).matches(itemsList)) { // here is() can be omitted
    // any
}
0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.