Behavior of allMatch, anyMatch and noneMatch for empty Stream

Brian Goetz brian.goetz at oracle.com
Wed Dec 12 06:15:38 PST 2012


These values are correct.

All/None match correspond to universal quantification, which can be 
vacuously satisfied; Any match corresponds to existential 
quantification, which cannot.

On 12/12/2012 6:26 AM, elena votchennikova wrote:
> 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