Flown is absolutely right in his comment. SORTED is only reported for the natural order, and this has been discussed previously. At first, it is even internally used as a flag called: isNaturalSort :
OfRef(AbstractPipeline<?, T, ?> upstream) { super(upstream, StreamShape.REFERENCE, StreamOpFlag.IS_ORDERED | StreamOpFlag.IS_SORTED); this.isNaturalSort = true;
The same flag isNaturalSort set to false when used through sorted(CustomComparator) .
These are internal details, and it seems that the jdk developers did not find useful for its implementation as such - perhaps it did not matter what could be really useful. But that can change ...
There is at least one drawback. Imagine a class like this:
static class User implements Comparable<User> { private final int id; public User(int id) { super(); this.id = id; } public int getId() { return id; } @Override public int compareTo(User usr) { return 42;
And some stream operations:
Stream<User> byId = Stream.of(new User(12), new User(10)) .sorted(Comparator.comparing(User::getId)); System.out.println(byId.spliterator().hasCharacteristics(Spliterator.SORTED)); Stream<User> natural = Stream.of(new User(12), new User(10)) .sorted(Comparator.naturalOrder()); System.out.println(natural.spliterator().hasCharacteristics(Spliterator.SORTED)); Stream<User> plain = Stream.of(new User(12), new User(10)).sorted(); System.out.println(plain.spliterator().hasCharacteristics(Spliterator.SORTED));
The first two reports are false , but the last reports true ; which is at least strange.
source share