Advanced Java: Collections, Generics and More ยท Ordering and Equality
Give a class a natural order with compareTo, and sort it any other way with a Comparator.
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
class Player implements Comparable<Player> {
String name;
int score;
Player(String name, int score) { this.name = name; this.score = score; }
public int compareTo(Player other) { // the natural order: by name
return name.compareTo(other.name);
}
public String toString() { return name + ":" + score; }
}
public class ComparatorDemo {
public static void main(String[] args) {
List<Player> players = new ArrayList<>(List.of(new Player("Cy", 95), new Player("Al", 70), new Player("Bo", 80)));
players.sort(null); // natural order (compareTo)
String byName = players.toString();
players.sort(Comparator.comparingInt((Player p) -> p.score).reversed()); // a different order
System.out.println(byName + " " + players);
}
}Player constructor to set up the new object, with name = "Cy" and score = 95. The program starts in main. args holds its command-line arguments (none here).Player() has finished, so its variables are gone.Player constructor to set up the new object, with name = "Al" and score = 70.Player() has finished, so its variables are gone.Player constructor to set up the new object, with name = "Bo" and score = 80.Player() has finished, so its variables are gone.players is created on the stack and points to a new ArrayList on the heap: [a Player, a Player, a Player].other is created on the stack and points to a new Player on the heap: {name: "Cy", score: 95}.class Player implements Comparable<Player> {.other is created on the stack and points to a new Player on the heap: {name: "Al", score: 70}.other goes out of scope and disappears.players points to, index 0 changes from a Player to a Player and index 1 changes from a Player to a Player.other is created on the stack and points to a new Player on the heap: {name: "Cy", score: 95}.other goes out of scope and disappears.class Player implements Comparable<Player> {.other is created on the stack and points to a new Player on the heap: {name: "Al", score: 70}.other goes out of scope and disappears.compareTo() has finished, so its variables are gone. In the ArrayList that players points to, index 1 changes from a Player to a Player and index 2 changes from a Player to a Player.toString(). It gets its own variables on the stack.public String toString() { return name + ":" + score; }.public String toString() { return name + ":" + score; }.toString() has finished, so its variables are gone.byName.p now points to a different Player: {name: "Bo", score: 80}.players.sort(Comparator.comparingInt((Player p) -> p.score).reversed()); // a different order.p now points to a different Player: {name: "Cy", score: 95}.lambda$main$0() has finished, so its variables are gone. In the ArrayList that players points to, index 0 changes from a Player to a Player and index 2 changes from a Player to a Player.toString(). It gets its own variables on the stack.public String toString() { return name + ":" + score; }.public String toString() { return name + ":" + score; }.toString() has finished, so its variables are gone.[Al:70, Bo:80, Cy:95] [Cy:95, Bo:80, Al:70].