简体   繁体   English

JAVA:请帮助我进行compareTo对象

[英]JAVA: Please help me with compareTo objects

How do I use a public int compareTo method to compare two objects? 如何使用public int compareTo方法比较两个对象? I need to compare the values within the object to the values of another object to test for greater/less than. 我需要将对象内的值与另一个对象的值进行比较,以测试大于/小于。 So comparing a in 2 objects and b in two objects respectfully. 因此,分别比较两个对象中的a in对象和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;
    }
}

Implements Compareable<T> interface to class Test . 实现类Test Compareable<T>接口。 Compareable interface is having a single method comapreTo which accepts the object to be compared. 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);
    }
}

This interface actually imposes a total ordering on the objects in collection of each class that implements it. 实际上,此接口对实现该接口的每个类的集合中的对象强加了总体排序。 This ordering is referred to as the class's natural ordering, and the class's compareTo method is referred to as its natural comparison method. 此排序称为类的自然排序,而该类的compareTo方法被称为其natural comparison方法。 Which helps Collection of Object to sort in certain order. 这有助于“对象集合”按特定顺序排序。

You need your class to implement the Comparable Interface and then override the compareTo method in your class. 您需要您的类来实现Comparable接口,然后在您的类中重写compareTo方法。 In this method you should do the appropriate comparison. 在这种方法中,您应该进行适当的比较。

You Test class should implement Comparable interface and override compareTo() method. 您的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.

}

First of all you have to implements Compareable<T> in your class because : From doc 首先,您必须在类中实现Compareable<T> ,因为:来自doc

This interface imposes a total ordering on the objects of each class that implements it. 该接口对实现该接口的每个类的对象强加了总体排序。 This ordering is referred to as the class's natural ordering, and the class's compareTo method is referred to as its natural comparison method. 此排序称为类的自然排序,而该类的compareTo方法被称为其自然比较方法。

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

  ...

}

您可以使用示例查看所有可比较和比较中的示例的详细信息

Comparision of objects can be done in java using either the interface Comparable/Comparator. 可以使用接口Comparable / Comparator在Java中完成对象的比较。 Comparable interface is used to specify the natural ordering of an object while the comparator is generally used by the programmer to change the natural ordering followed by the particular object and specify his sorting preference 可比较接口用于指定对象的自然顺序,而程序员通常使用比较器来更改特定对象之后的自然顺序并指定其排序优先级

In your example 在你的例子中

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 and comparator are usually used for sorting. 比较器和比较器通常用于排序。 This can be illustrated in your example as below 可以在下面的示例中说明

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

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM