Behavior of allMatch, anyMatch and noneMatch for empty Stream

elena votchennikova elena.votchennikova at oracle.com
Wed Dec 12 03:26:55 PST 2012


Hi,

please clarify behavior of allMatch(Predicate), anyMatch(Predicate) and 
noneMatch(Predicate) methods for empty Stream.

1) Please look at the next code:
System.out.println(Streams.emptyStream().allMatch(o -> true));
System.out.println(Streams.emptyStream().anyMatch(o -> true));
System.out.println(Streams.emptyStream().noneMatch(o -> true));

The result output will be:
true
false
true


Is it OK? And will such behavior be specified?

2) Methods allMatch, anyMatch and noneMatch throw NullPointerException 
if Predicate is null, for example:
Stream notEmptyStream = Arrays.stream(new Object[] {new Object()});
notEmptyStream.allMatch(null);

But if Stream is empty methods will not throw NPE.
So, next code will print same result as in point 1)
System.out.println(Streams.emptyStream().allMatch(null));
System.out.println(Streams.emptyStream().anyMatch(null));
System.out.println(Streams.emptyStream().noneMatch(null));

Is it OK or calling these methods with null-Predicate should throws NPE?



Best regards,
Elena


More information about the lambda-dev mailing list