2013-01-22 27 views
1

如何使用public int compareTo方法比較兩個對象?我需要將對象內的值與另一個對象的值進行比較以測試更大/更小的值。因此恭敬地比較兩個對象中的b和兩個對象中的b。JAVA:請幫我用compareTo對象

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

實現接口'Comparable':設置以及一個示例:http://docs.oracle.com/javase/6/docs/ api/java/lang/Comparable.html – home

+0

您想如何比較這些對象?哪一個對象會更大?a = new Test(1,2)'或'b = new Test(2,1)'? – Pshemo

+0

要比較哪些屬性? – xyz

回答

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方法。這有助於Collection對象按照一定的順序排序。

0

您需要您的類實現可比較接口,然後重寫類中的compareTo方法。在這種方法中,你應該做適當的比較。

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. 

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

    ... 

} 
0

對象的比較可以在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; 
} 

}