Behavior of allMatch, anyMatch and noneMatch for empty Stream

Peter Levart peter.levart at gmail.com
Wed Dec 12 08:21:55 PST 2012


On 12/12/2012 04:44 PM, Olexandr Demura wrote:
> By some unknown reason I percept null to be effectively equal to any chosen
> SAM with method defined to throw NPE.
> e.g. Streams.emptyStream().anyMatch(o -> ((Boolean) null).booleanValue())
Predicate p1 = null;
Predicate p2 = o -> ((Boolean) null).booleanValue();

p1 and p2 are indistinguishable if their identities and Object-ness-es 
are ignored. And we all know we should not compare identities and call 
Object methods of functions...

Peter
>
> 2012/12/12 Vitaly Davidovich <vitalyd at gmail.com>
>
>> Brian,
>>
>> I think NPE should be thrown when predicate is null irrespective of stream
>> length for consistency sake.
>>
>> Thanks
>>
>> Sent from my phone
>> On Dec 12, 2012 9:16 AM, "Brian Goetz" <brian.goetz at oracle.com> wrote:
>>
>>> 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?
>>



More information about the lambda-dev mailing list