1

如何使用public int compareTo方法比较两个对象?我需要将对象中的值与另一个对象的值进行比较以测试大于/小于。因此,分别比较两个对象中的 a 和两个对象中的 b。

Test x1 = new Test(9999,9999);
Test x2 = new Test(0,0);

public class Test{
    private int a;
    private int b;

    public Test(){
        a = 0;
        b = 0;
    }


    public Test(int a, int b){
        this.a = a;
        this.b = b;
    }
}
4

6 回答 6

1

实现Compareable<T>类的接口TestCompareable接口有一个comapreTo接受要比较的对象的方法。

class Test implements Compareable<Test>{
    ...
    @Override
    public int compareTo(Test test) {
        // write logic for compare 
        //a negative integer, zero, or a positive integer as 
        //this object is less than, equal to, or greater than the specified object
        return 0;
    }
}

class Main{
    public static void main(String[] args){
        Test x1 = new Test(9999,9999);
        Test x2 = new Test(0,0);
        int x3 = x1.compareTo(x2);
    }
}

这个接口实际上对实现它的每个类的集合中的对象进行了总排序。这种排序称为类的自然排序,类的compareTo方法称为它的natural comparison方法。这有助于对象集合按特定顺序排序。

于 2013-01-22T06:50:52.010 回答
0

您需要您的类来实现 Comparable 接口,然后在您的类中覆盖 compareTo 方法。在这种方法中,您应该进行适当的比较。

于 2013-01-22T06:51:03.680 回答
0

您的测试类应该实现 Comparable 接口并覆盖 compareTo() 方法。

int compareTo(Test o){

 // return a negative integer, zero, or a positive integer as per your logic.

// Less than means calling object is less than o.

}
于 2013-01-22T06:55:58.127 回答
0

首先你必须Compareable<T>在你的类中实现,因为:来自doc

该接口对实现它的每个类的对象进行了总排序。这种排序称为类的自然排序,类的 compareTo 方法称为其自然比较方法。

CompareTo的合同

class Test implements Compareable<Test>{

    @Override
    public int compareTo(Test test) {
        // write logic for compare  based on the contract of compareTo
        return 0;
    }

  ...

}
于 2013-01-22T06:56:47.377 回答
0

您可以通过示例查看所有可比较和比较器的详细信息

于 2013-01-22T06:58:18.883 回答
0

对象的比较可以在 java 中使用接口 Comparable/Comparator 来完成。Comparable 接口用于指定对象的自然顺序,而比较器通常由程序员用于更改特定对象遵循的自然顺序并指定其排序偏好

在你的例子中

public class Test implements Comparable<Test> {
private final int a;
private final int b;

public static void main(String[] args) {
    Test x1 = new Test(9999, 9999);
    Test x2 = new Test(0, 0);

    int comparisionVal = x1.compareTo(x2);

    System.out.println(comparisionVal > 0 ? "First object is greater"
            : (comparisionVal < 0 ? "Second object is greater"
                    : "both are equal"));

}

public Test() {
    this.a = 0;
    this.b = 0;
}

public Test(int a, int b) {
    this.a = a;
    this.b = b;
}

@Override
public int compareTo(Test o) {
    return this.a - o.a; // ascending based on a
    // return this.a - o.a; // descending based on a
}

@Override
public String toString() {
    return "a = " + this.a + "; b = " + this.b;
}

}

Comparable 和comparator 通常用于排序。这可以在您的示例中进行说明,如下所示

public class Test implements Comparable<Test> {
private final int a;
private final int b;

public static void main(String[] args) {
    Test x1 = new Test(9999, 9999);
    Test x2 = new Test(0, 0);
    Test x3 = new Test(4444, 4444);
    Test x4 = new Test(555, 555);

    List<Test> list = new ArrayList<Test>();

    list.add(x1);
    list.add(x2);
    list.add(x3);
    list.add(x4);

    Collections.sort(list);

    System.out.println("The object in ascending order: " + list);

    // If you wish to do a descending sort that is where you'd use
    // comparator
    Collections.sort(list, new Comparator<Test>() {
        @Override
        public int compare(Test o1, Test o2) {
            return o2.a - o1.a;
        }
    });
    System.out.println("The object in descending order: " + list);

}

public Test() {
    this.a = 0;
    this.b = 0;
}

public Test(int a, int b) {
    this.a = a;
    this.b = b;
}

@Override
public int compareTo(Test o) {
    return this.a - o.a; // ascending based on a
    // return this.a - o.a; // descending based on a
}

@Override
public String toString() {
    return "a = " + this.a + "; b = " + this.b;
}

}

于 2013-01-22T07:28:35.083 回答