RFR: 8272992: Replace usages of Collections.sort with List.sort call in jdk.* modules

Rémi Forax forax at openjdk.java.net
Mon Sep 13 18:02:56 UTC 2021


On Mon, 23 Aug 2021 21:08:05 GMT, Andrey Turbanov <github.com+741251+turbanoff at openjdk.org> wrote:

> Collections.sort is just a wrapper, so it is better to use an instance method directly.

src/jdk.jfr/share/classes/jdk/jfr/internal/MetadataReader.java line 86:

> 84:         if (Logger.shouldLog(LogTag.JFR_SYSTEM_PARSER, LogLevel.TRACE)) {
> 85:              List<Type> ts = new ArrayList<>(types.values());
> 86:              ts.sort((x, y) -> x.getName().compareTo(y.getName()));

you can use Comparator.comparing(Type::getName)

src/jdk.jfr/share/classes/jdk/jfr/internal/SettingsManager.java line 142:

> 140:         } else {
> 141:             if (Logger.shouldLog(LogTag.JFR_SETTING, LogLevel.INFO)) {
> 142:                 eventControls.sort((x, y) -> x.getEventType().getName().compareTo(y.getEventType().getName()));

Comparator.comparing(x -> e.getEventType().getName())

src/jdk.jfr/share/classes/jdk/jfr/internal/TypeLibrary.java line 111:

> 109:                 try {
> 110:                     jvmTypes = MetadataLoader.createTypes();
> 111:                     jvmTypes.sort((a, b) -> Long.compare(a.getId(), b.getId()));

Comparator.comparingLong(Type::getId)

src/jdk.jfr/share/classes/jdk/jfr/internal/consumer/RepositoryFiles.java line 215:

> 213:                 pathLookup.remove(remove);
> 214:             }
> 215:             added.sort((p1, p2) -> p1.compareTo(p2));

'added.sort(Path::compareTo)'

src/jdk.jfr/share/classes/jdk/jfr/internal/dcmd/DCmdCheck.java line 137:

> 135:         List<EventType> sorted = new ArrayList<>();
> 136:         sorted.addAll(events);
> 137:         sorted.sort(new Comparator<EventType>() {

I wonder if there is a bootstrap issue here (why an anonymous class is used instead of a lambda?)
If a lambda can be used, it cn be simplified to
`sorted.sort(Comparator.comparing(EventType::getName))`

src/jdk.jfr/share/classes/jdk/jfr/internal/tool/Summary.java line 145:

> 143:             println(" Duration: " + (totalDuration + 500_000_000) / 1_000_000_000 + " s");
> 144:             List<Statistics> statsList = new ArrayList<>(stats.values());
> 145:             statsList.sort((u, v) -> Long.compare(v.count, u.count));

`statsList.sort(Comparator.comparingLong(v -> v.count))`

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

PR: https://git.openjdk.java.net/jdk/pull/5230


More information about the hotspot-dev mailing list