JAVA:请帮我一下compareTo对象

时间:2013-01-22 06:47:22

标签: java constructor

如何使用public int compareTo方法比较两个对象?我需要将对象中的值与另一个对象的值进行比较,以测试更大/更小的值。因此,分别比较两个对象和两个对象中的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;
    }
}

6 个答案:

答案 0 :(得分:1)

实现Compareable<T>Test的接口。 Compareable接口有一个方法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方法。这有助于集合对象按特定顺序排序。

答案 1 :(得分:0)

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

答案 2 :(得分:0)

You Test类应该实现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.

}

答案 3 :(得分: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;
    }

  ...

}

答案 4 :(得分:0)

你可以check in details with example 为所有人提供可比较和比较的集合

答案 5 :(得分:0)

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

在你的例子中

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

}

可比较和比较器通常用于排序。这可以在您的示例中说明,如下所示

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

}