On Tue, 11 Nov 2025 16:34:21 GMT, Patrick Strawderman <[email protected]> wrote:

>> When `Comparator.naturalOrder()` was explicitly supplied to a collection 
>> such as `TreeSet`, or passed into the `sorted` method of a stream, the 
>> sorted characteristic was not preserved, causing unnecessary buffering and 
>> duplicate sorting.
>> 
>> Example:
>> 
>> 
>> TreeSet<Integer> sortedSet = new TreeSet<>(Comparator.naturalOrder());
>> sortedSet.add(1);
>> sortedSet.add(2);
>> // SortedOps.OfRef.opWrapSink is not a no-op
>> sortedSet.stream().sorted().forEach(System.out::println);
>> 
>> or
>> 
>> TreeSet<Integer> sortedSet = new TreeSet<>();
>> sortedSet.add(1);
>> sortedSet.add(2);
>> // SortedOps.OfRef.opWrapSink is not a no-op
>> sortedSet.stream().sorted(Comparator.naturalOrder()).forEach(System.out::println);
>> 
>> 
>> This PR updates `SortedOps.makeRef` and `StreamOpFlag.fromCharacteristics` 
>> to handle the above cases and avoid the unnecessary sort step.
>
> Patrick Strawderman has updated the pull request incrementally with one 
> additional commit since the last revision:
> 
>   Update src/java.base/share/classes/java/util/stream/StreamOpFlag.java
>   
>   Simplify comparison
>   
>   Co-authored-by: Viktor Klang <[email protected]>

I wonder if it's not better to replace Comparator.naturalOrder() by null in the 
constructor of TreeSet, given that TreeSet does not provide a getter for it so 
the only way to get the comparator is using 
treeSet.spliterator().getComparator().

-------------

PR Comment: https://git.openjdk.org/jdk/pull/28226#issuecomment-3517882648

Reply via email to