The javadoc of Spliterator
(which is basically what is really behind a Stream
if I understand things correctly) defines many characeristics which make sense such as SIZED
, CONCURRENT
, IMMUTABLE
etc.
But it also defines NONNULL
; why?
I'd have though that it would be the user's responsibility to ensure that and that if, for instance, a developer tried to .sort()
a non SORTED
stream where there are null elements he/she would rightfully be greeted with an NPE...
But then this characteristic exists. Why? The javadoc of Spliterator
itself doesn't mention any real usage of it, and neither does the package-info.java
of the java.util.stream
package...
From the documentation of Spliterator
:
A Spliterator also reports a set of
characteristics()
of its structure, source, and elements from amongORDERED
,DISTINCT
,SORTED
,SIZED
,NONNULL
,IMMUTABLE
,CONCURRENT
, andSUBSIZED
. These may be employed by Spliterator clients to control, specialize or simplify computation.
Note that it does not mention the prevention of NullPointerException
s. If you sort a Stream
which might contain null
values it is your responsibility to provide a Comparator
which can handle null
s.
The second sentence also makes it clear that using these flags is only an option, not a requirement for “Spliterator clients”, which is not limited to usage by Stream
s.
So regardless of whether it is used by the current implementation of the Stream
API, are there possibilities to gain advantage of the knowledge about a NONULL
characteristic?
I think so. An implementation may branch to a specialized code for a non-null
Spliterator
to utilize null
for representing certain state then, e.g. absent values or the initial value before processing the first element, etc. If fact, the actual implementation code for dealing with Stream
s which may contain null
is complicated. But of course, you always have to weigh up whether the simplification of one case justifies the code duplication.
But sometimes the simplification is as simple as knowing that there are no null
values implies that you can use one of the Concurrent…
Collections, which don’t allow null
s, internally.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With