Sorting streams containing nulls

Brian Goetz brian.goetz at oracle.com
Thu Nov 8 05:47:38 PST 2012


This implementation may be changed, but I find it hard to imagine that 
in the general case, sorting streams with nulls will not throw NPE 
somewhere.  (For example, most comparators do not check for null.)

On 11/8/2012 4:51 AM, Georgiy Rakov wrote:
> I could suppose that NPE is thrown because of the sorted()
> implementation - see stack trace. It uses PriorityQueue for internal
> purpose; PriorityQueue doesn't support nulls that's why NPE is thrown.
> If I understand you correctly this is supposed to be fixed, could you
> please tell if it's really so.
>
> Georgiy.
>
> On 07.11.2012 17:30, Brian Goetz wrote:
>> The exact specification is not yet written, but we are not expecting
>> stream implementations or operations to do anything special with
>> nulls.  This means nulls may be passed to lambdas or inserted into
>> collections that do not support them, resulting in NPE.
>>
>>
>>
>> On Nov 7, 2012, at 5:22 AM, Georgiy Rakov wrote:
>>
>>> Hello.
>>>
>>> When we make sorted(...).iterator() on Stream instance containing one
>>> ore more nulls we receive NPE. The example of stack trace is below:
>>>
>>>     java.lang.NullPointerException
>>>          at java.util.PriorityQueue.offer(PriorityQueue.java:320)
>>>          at java.util.PriorityQueue.add(PriorityQueue.java:306)
>>>          at java.util.streams.ops.SortedOp.iterator(SortedOp.java:105)
>>>          at
>>> java.util.streams.ops.SortedOp.wrapIterator(SortedOp.java:97)
>>>          at
>>>
>>> java.util.streams.AbstractPipeline.iterator(AbstractPipeline.java:329)
>>>          ...
>>>
>>> Could you please tell if it is considered as expected behavior or it's
>>> going to be fixed somehow.
>>>
>>> Georgiy.
>>>


More information about the lambda-dev mailing list