Behavior of allMatch, anyMatch and noneMatch for empty Stream
Remi Forax
forax at univ-mlv.fr
Wed Dec 12 08:48:06 PST 2012
On 12/12/2012 03:22 PM, Vitaly Davidovich wrote:
> Brian,
>
> I think NPE should be thrown when predicate is null irrespective of stream
> length for consistency sake.
>
> Thanks
yes, it this should be true for any parameters that are typed by a
functional interface at least in java.util and java.util.stream.
We don't want user to send null as a lambda exactly like we don't want
user to send a null enum.
Rémi
>
> 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?
>>>
>>>
>>>
>>> Best regards,
>>> Elena
>>>
>>
More information about the lambda-dev
mailing list