RFR: 8362958: Unnecessary copying / sorting in Streams using Comparator.naturalOrder() [v2]

Patrick Strawderman duke at openjdk.org
Tue Nov 11 17:13:06 UTC 2025


On Tue, 11 Nov 2025 16:56:18 GMT, Rémi Forax <forax at openjdk.org> wrote:

> 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().

That wouldn't help with the `stream().sorted(Comparator.naturalOrder())` case; the example I supplied is somewhat contrived, I originally ran into this in Guava (see the [issue](https://github.com/google/guava/issues/6187) I opened there); the library is null-hostile and so always supplies a comparator to its sorted collections. Note that this won't directly fix the issue there until they move away from their custom natural order comparator to the one from the JDK.

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

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


More information about the core-libs-dev mailing list