如何编写比较多个参数的比较器?

我正在尝试编写一个比较器来比较 Coordinate 类的两个对象。Coordinate 类非常简单:


public class Coordinate {


    private int x, y;


    public Coordinate(int x, int y) {

        this.x = x;

        this.y = y;

    }


    public int getX() {

        return x;

    }


    public void setX(int x) {

        this.x = x;

    }


    public int getY() {

        return y;

    }


    public void setY(int y) {

        this.y = y;

    }


}

现在我想让 Comparator 比较 Coordinate 类的两个实例的 x 和 y 值。这是一个例子:

我有一个坐标 c1,它有 x = 42 和 y = 23。我的第二个坐标 c2 有 x = 23 和 y = 54。现在我把它们都放在一个 ArrayList 中,想要对列表进行排序。我想按以下方式排序:

具有最低 y 值的坐标始终排在第一位,当您有两个具有相同 y 值的坐标时,该坐标排在第一位,其 x 值较低。

例子:


c1 (y = 4, x = 5 ) < c2 (y = 4, x = 6) < c3 (y = 5, x = 2)  

那么我怎样才能为此目的编写一个比较器呢?


跃然一笑
浏览 100回答 2
2回答

慕的地8271018

Comparator<Coordinate> c = Comparator.comparingInt(Coordinate::getY)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.thenComparingInt(Coordinate::getX);您可以通过thenComparing和构建复合比较器thenComparingX。var list = List.of(&nbsp; &nbsp; &nbsp; &nbsp; new Coordinate(6, 4),&nbsp; &nbsp; &nbsp; &nbsp; new Coordinate(2, 5),&nbsp; &nbsp; &nbsp; &nbsp; new Coordinate(5, 4));list.sort(c);System.out.println(list);片段打印[{y=4, x=5}, {y=4, x=6}, {y=5, x=2}]

30秒到达战场

使用比较器import java.util.ArrayList;import java.util.Comparator;class Coordinate {&nbsp; &nbsp; private int x, y;&nbsp; &nbsp; public Coordinate(int x, int y) {&nbsp; &nbsp; &nbsp; &nbsp; this.x = x;&nbsp; &nbsp; &nbsp; &nbsp; this.y = y;&nbsp; &nbsp; }&nbsp; &nbsp; public int getX() {&nbsp; &nbsp; &nbsp; &nbsp; return x;&nbsp; &nbsp; }&nbsp; &nbsp; public void setX(int x) {&nbsp; &nbsp; &nbsp; &nbsp; this.x = x;&nbsp; &nbsp; }&nbsp; &nbsp; public int getY() {&nbsp; &nbsp; &nbsp; &nbsp; return y;&nbsp; &nbsp; }&nbsp; &nbsp; public void setY(int y) {&nbsp; &nbsp; &nbsp; &nbsp; this.y = y;&nbsp; &nbsp; }&nbsp; &nbsp; public String toString() {&nbsp; &nbsp; &nbsp; &nbsp; return "x = " + x + " y = " + y;&nbsp; &nbsp; }}public class Temp {&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; ArrayList<Coordinate> A = new ArrayList<>();&nbsp; &nbsp; &nbsp; &nbsp; A.add(new Coordinate(1, 2));&nbsp; &nbsp; &nbsp; &nbsp; A.add(new Coordinate(2, 1));&nbsp; &nbsp; &nbsp; &nbsp; A.add(new Coordinate(3, 2));&nbsp; &nbsp; &nbsp; &nbsp; A.sort(new Comparator<Coordinate>() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public int compare(Coordinate o1, Coordinate o2) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (o1.getY() < o2.getY()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return -1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else if (o1.getY() > o2.getY()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (o1.getX() < o2.getX()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return -1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else if (o1.getX() > o2.getX()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(A.toString());&nbsp; &nbsp; }}使用可比较的接口import java.util.ArrayList;class Coordinate implements Comparable<Coordinate> { # Notice implementing Comparable interface&nbsp; &nbsp; private int x, y;&nbsp; &nbsp; public Coordinate(int x, int y) {&nbsp; &nbsp; &nbsp; &nbsp; this.x = x;&nbsp; &nbsp; &nbsp; &nbsp; this.y = y;&nbsp; &nbsp; }&nbsp; &nbsp; public int getX() {&nbsp; &nbsp; &nbsp; &nbsp; return x;&nbsp; &nbsp; }&nbsp; &nbsp; public void setX(int x) {&nbsp; &nbsp; &nbsp; &nbsp; this.x = x;&nbsp; &nbsp; }&nbsp; &nbsp; public int getY() {&nbsp; &nbsp; &nbsp; &nbsp; return y;&nbsp; &nbsp; }&nbsp; &nbsp; public void setY(int y) {&nbsp; &nbsp; &nbsp; &nbsp; this.y = y;&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public int compareTo(Coordinate o) { # implementing the abstract method of Comparable interface&nbsp; &nbsp; &nbsp; &nbsp; if (y < o.y) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return -1;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; } else if (y > o.y) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return 1;&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (x < o.x) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return -1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else if (x > o.x) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return 0;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; public String toString() {&nbsp; &nbsp; &nbsp; &nbsp; return "x = " + x + " y = " + y;&nbsp; &nbsp; }}public class Temp {&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; ArrayList<Coordinate> A = new ArrayList<>();&nbsp; &nbsp; &nbsp; &nbsp; A.add(new Coordinate(1, 2));&nbsp; &nbsp; &nbsp; &nbsp; A.add(new Coordinate(2, 1));&nbsp; &nbsp; &nbsp; &nbsp; A.add(new Coordinate(3, 2));&nbsp; &nbsp; &nbsp; &nbsp; A.sort(null);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(A.toString());&nbsp; &nbsp; }}输出[x = 2 y = 1, x = 1 y = 2, x = 3 y = 2]
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java