Как @ user6158055 suggets, это один вкладыш с Java 8
, как показано ниже:
Collections.sort(
hockeyPlayerList,
(player1, player2) -> player1.getGoalsScored()
- player2.getGoalsScored());
Полный пример, чтобы изобразить то же самое:
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Main {
public static void main(String[] args) {
List<HockeyPlayer> hockeyPlayerList = new ArrayList<>();
hockeyPlayerList.add(new HockeyPlayer("A", 3));
hockeyPlayerList.add(new HockeyPlayer("D", 10));
hockeyPlayerList.add(new HockeyPlayer("B", 2));
System.out.println("Before Sort based on goalsScored\n");
hockeyPlayerList.forEach(System.out::println);
System.out.println("\nAfter Sort based on goalsScored\n");
Collections.sort(
hockeyPlayerList,
(player1, player2) -> player1.getGoalsScored()
- player2.getGoalsScored());
hockeyPlayerList.forEach(System.out::println);
}
static class HockeyPlayer {
private String name;
private int goalsScored;
public HockeyPlayer(final String name, final int goalsScored) {
this.name = name;
this.goalsScored = goalsScored;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getGoalsScored() {
return goalsScored;
}
public void setGoalsScored(int goalsScored) {
this.goalsScored = goalsScored;
}
@Override
public String toString() {
return "HockeyPlayer [name=" + name + ", goalsScored="
+ goalsScored + "]";
}
}
}
Вывод :
Before Sort based on goalsScored
HockeyPlayer [name=A, goalsScored=3]
HockeyPlayer [name=D, goalsScored=10]
HockeyPlayer [name=B, goalsScored=2]
After Sort based on goalsScored
HockeyPlayer [name=B, goalsScored=2]
HockeyPlayer [name=A, goalsScored=3]
HockeyPlayer [name=D, goalsScored=10]