Advanced Java: Collections, Generics and More ยท Ordering and Equality

Comparable and Comparator

Give a class a natural order with compareTo, and sort it any other way with a Comparator.

ComparatorDemo.java

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);
    }
}

Step by step

  1. Java runs the 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).
  2. Player() has finished, so its variables are gone.
  3. Java runs the Player constructor to set up the new object, with name = "Al" and score = 70.
  4. Player() has finished, so its variables are gone.
  5. Java runs the Player constructor to set up the new object, with name = "Bo" and score = 80.
  6. Player() has finished, so its variables are gone.
  7. players is created on the stack and points to a new ArrayList on the heap: [a Player, a Player, a Player].
  8. sort(null) uses the natural order: Player's compareTo, by name.
  9. other is created on the stack and points to a new Player on the heap: {name: "Cy", score: 95}.
  10. Comparing two names.
  11. Line runs: class Player implements Comparable<Player> {.
  12. other is created on the stack and points to a new Player on the heap: {name: "Al", score: 70}.
  13. other goes out of scope and disappears.
  14. In the ArrayList that players points to, index 0 changes from a Player to a Player and index 1 changes from a Player to a Player.
  15. other is created on the stack and points to a new Player on the heap: {name: "Cy", score: 95}.
  16. other goes out of scope and disappears.
  17. Line runs: class Player implements Comparable<Player> {.
  18. other is created on the stack and points to a new Player on the heap: {name: "Al", score: 70}.
  19. other goes out of scope and disappears.
  20. 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.
  21. Java calls toString(). It gets its own variables on the stack.
  22. Line runs: public String toString() { return name + ":" + score; }.
  23. Line runs: public String toString() { return name + ":" + score; }.
  24. toString() has finished, so its variables are gone.
  25. The value it returned, "[Al:70, Bo:80, Cy:95]", is stored in byName.
  26. A Comparator gives a different order: by score, highest first.
  27. p now points to a different Player: {name: "Bo", score: 80}.
  28. Line runs: players.sort(Comparator.comparingInt((Player p) -> p.score).reversed()); // a different order.
  29. p now points to a different Player: {name: "Cy", score: 95}.
  30. 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.
  31. Java calls toString(). It gets its own variables on the stack.
  32. Line runs: public String toString() { return name + ":" + score; }.
  33. Line runs: public String toString() { return name + ":" + score; }.
  34. toString() has finished, so its variables are gone.
  35. It prints [Al:70, Bo:80, Cy:95] [Cy:95, Bo:80, Al:70].
  36. By name [Al, Bo, Cy]; by score [Cy, Bo, Al]. One class, two orders.