【问题标题】:How can I write a Comparator comparing multiple arguments? [duplicate]如何编写比较多个参数的比较器? [复制]
【发布时间】:2019-03-19 01:07:07
【问题描述】:

我正在尝试编写一个比较器来比较坐标类的两个对象。 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)  

那么我该如何为此目的编写比较器呢?
非常感谢!

【问题讨论】:

标签: java comparison comparator


【解决方案1】:
Comparator<Coordinate> c = Comparator.comparingInt(Coordinate::getY)
                                     .thenComparingInt(Coordinate::getX);

您可以通过thenComparingthenComparingX 构建复合比较器。

var list = List.of(
        new Coordinate(6, 4),
        new Coordinate(2, 5),
        new Coordinate(5, 4)
);

list.sort(c);
System.out.println(list);

sn-p 打印

[{y=4, x=5}, {y=4, x=6}, {y=5, x=2}]

【讨论】:

    【解决方案2】:
    • 使用比较器


    import java.util.ArrayList;
    import java.util.Comparator;
    
    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;
        }
    
        public String toString() {
            return "x = " + x + " y = " + y;
        }
    }
    
    public class Temp {
        public static void main(String[] args) {
            ArrayList<Coordinate> A = new ArrayList<>();
            A.add(new Coordinate(1, 2));
            A.add(new Coordinate(2, 1));
            A.add(new Coordinate(3, 2));
            A.sort(new Comparator<Coordinate>() {
    
                @Override
                public int compare(Coordinate o1, Coordinate o2) {
                    if (o1.getY() < o2.getY()) {
                        return -1;
                    } else if (o1.getY() > o2.getY()) {
                        return 1;
                    } else {
                        if (o1.getX() < o2.getX()) {
                            return -1;
                        } else if (o1.getX() > o2.getX()) {
                            return 1;
                        }
                        return 0;
                    }
                }
            });
            System.out.println(A.toString());
        }
    }
    
    • 使用可比接口


    import java.util.ArrayList;
    
    class Coordinate implements Comparable<Coordinate> { # Notice implementing Comparable interface
        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;
        }
    
        @Override
        public int compareTo(Coordinate o) { # implementing the abstract method of Comparable interface
            if (y < o.y) {
                return -1; 
            } else if (y > o.y) {
                return 1;
            } else {
                if (x < o.x) {
                    return -1;
                } else if (x > o.x) {
                    return 1;
                }
                return 0;
            }
        }
    
        public String toString() {
            return "x = " + x + " y = " + y;
        }
    }
    
    public class Temp {
        public static void main(String[] args) {
            ArrayList<Coordinate> A = new ArrayList<>();
            A.add(new Coordinate(1, 2));
            A.add(new Coordinate(2, 1));
            A.add(new Coordinate(3, 2));
            A.sort(null);
            System.out.println(A.toString());
        }
    }
    


    输出

    [x = 2 y = 1, x = 1 y = 2, x = 3 y = 2]
    

    【讨论】:

      猜你喜欢
      • 2023-03-30
      • 2021-10-04
      • 2019-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-24
      • 1970-01-01
      • 2020-04-10
      相关资源
      最近更新 更多