Lovely Java Improvements – Thanks @JetBrains #IntelliJ

On February 13, 2018 by aTutorMe

Back in the day, this is how I would sort an array of file names in alphabetical order:


secondaryTaskConfigFiles.sort(new Comparator() {
@Override
public int compare(File o1, File o2) {
return o1.getName().compareTo(o2.getName());
}
});

So IntelliJ told me to change the code to a lambda expression:


secondaryTaskConfigFiles.sort((o1, o2) -> o1.getName().compareTo(o2.getName()));

Looks nice? Yep! …But wait… another suggestion:


secondaryTaskConfigFiles.sort(Comparator.comparing(File::getName));

Thanks JetBrains for a fantastic IDE. For programmers used to the “old” ways it helps a lot!