Streams and Spliterator characteristics confusion

Kasper Nielsen kasperni at gmail.com
Fri Jun 27 07:06:13 UTC 2014


Hi,

I'm trying to understand how the Spliterator.characteristics are maintained
across stream operations and I'm a bit confused. Maybe someone here can
clear it up for me

s.sorted().spliterator() -> Spliterator.SORTED = true
But if I use specify a comparator the stream is not sorted
s.sorted((a,b) -> 1).spliterator() -> Spliterator.SORTED = false

s.distinct().spliterator() -> Spliterator.DISTINCT = true
but limiting the number of distinct elements makes the stream non distinct
s.distinct().limit(10).spliterator() -> Spliterator.DISTINCT = false
On the other hand something like Spliterator.SORTED is maintained when I
invoke limit
s.sorted().limit(10).spliterator() -> Spliterator.SORTED = true


A flag such as Spliterator.NONNULL is also cleared in situations where it
should not be.
In my opinion an operation such as filter(predicate) should not flip the
flag.

Finally, are there any relationship between Spliterator.SORTED and
Spliterator.ORDERED.
I would think that a stream cannot be sorted without it also being ordered?
but s.sorted().unordered().spliterator returns ORDERED=false and SORTED=true

Cheers
  Kasper



More information about the core-libs-dev mailing list