Search code examples
javajava-stream

Stream of null elements returns true


I have a simple validator method. Why the stream of null elements returns true? How does it works?

What happens after filtering on null?

private static boolean isNotValidCeo(List<Ceo> ceos) {
    if (CollectionUtils.isEmpty(ceos)) {
        return true;
    }

    return ceos.stream()
            .filter(Objects::nonNull)
            .map(Ceo::getSuperBonus)
            .allMatch(Objects::isNull);
}

the example by debugger


Solution

  • .filter(Objects::nonNull) results in empty set and allMatch on an empty set is true, because there are no elements to contradict the rule.