【问题标题】:How do you compare multiple properties of objects within an ArrayList using the Comparable Interface? [duplicate]如何使用 Comparable Interface 比较 ArrayList 中对象的多个属性? [复制]
【发布时间】:2018-08-10 01:11:48
【问题描述】:

我需要对我的 ArrayList list1 中的 Rectangle 对象按其相应的属性进行排序:heightwidthtopCorner(Point)。 在评估一个属性时,我可以对列表进行排序。

如何设置我的compareTo 方法,使其首先尝试按height 对列表中的对象进行排序,然后按width(如果所有对象高度相等),最后按topCorner (如果所有对象的高度和宽度都相等)?

public class Point implements Comparable<Point> {

private int x;
private int y;

public Point(){
    this(0,0);
}
public Point(int x,int y){
    this.x=x;
    this.y=y;
}
public int getX(){
    return x;
}
public int getY(){
    return y;
}
public int compareTo(Point pt){
    if(x==pt.x){
        return y-pt.y;
    }
    else{
        return x-pt.x;
    }
}
public String toString(){
    return "("+x+", "+y+")";
}
}
class Rectangle implements Comparable<Rectangle> {

private int height;
private int width;
private Point topCorner;

public Rectangle(int x,int y,int height,int width){
    this.height=height;
    this.width=width;
    this.topCorner=new Point(x,y);
}
public int getHeight(){
    return height;
}
public int getWidth(){
    return width;
}
public Point getPoint(){
    return topCorner;
}
public int compareTo(Rectangle rect){
    if(height!=rect.height){
        int compareHeight=((Rectangle)rect).getHeight();
        return this.height-compareHeight;
    }
    else if(width!=rect.width){
        int compareWidth=((Rectangle)rect).getWidth();
        return this.width-compareWidth;
    }
    else if(topCorner!=rect.topCorner){
        Point comparePoint=((Rectangle)rect).getPoint();
        return this.topCorner-topCorner;
    }
    else{
        System.out.println("// ERROR BRO // ERROR BRO //");
    }
    return 0;
}
public String toString(){
    return "(H:"+height+", W:"+width+", P:"+topCorner+")";
}
}

================================================ ==============================

public class RectangleComparable {
public static void main(String[]args){
    Random rn=new Random(21);
    ArrayList<Rectangle> list1=new ArrayList<>();
    for(int index=0;index<10;index++){
        int ran1=rn.nextInt(21), ran2=rn.nextInt(21),
                ran3=rn.nextInt(21), ran4=rn.nextInt(21);
        list1.add((new Rectangle(5,ran2,ran3,ran4)));
    }
    System.out.println("KEY : H=height, W=width, P=point\n");
    System.out.println("Unsorted List : \n"+list1+"\n");
    Collections.sort(list1);
    System.out.println("Sorted List : \n"+list1);
}
}

如果这是一个重复的问题,我会继续道歉。我环顾四周,但没有找到任何可以给我一个真正直接答案的东西(因为我仍然是一个编程菜鸟)。

【问题讨论】:

    标签: java arraylist collections compareto


    【解决方案1】:

    您可以编写自己的Comparator 或按如下方式构建:

    Comparator
        .comparingInt(Rectangle::getHeight)
        .thenComparingInt(Rectangle::getWidth)
        .thenComparing(Rectangle::getTopCorner);
    

    【讨论】:

      猜你喜欢
      • 2013-08-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多