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

Rémi Forax forax at openjdk.org
Tue Nov 11 17:49:02 UTC 2025


On Tue, 11 Nov 2025 17:10:34 GMT, Patrick Strawderman <duke at openjdk.org> wrote:

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

If you switch from Comparator.naturalOrder()) to null, you have to do it in stream.sorted() too (and also List.sort(Comparator), Collections.sort(Comparator), Array.sort(Comparator), Collections.reverseOrder(Comparator) etc)

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

yes, comparing comparators with == is brittle anyway, but at least you can make it consistent for the JDK.

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

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


More information about the core-libs-dev mailing list