2012-05-09 62 views
2

的發展我有我開發了一個POJO查詢..對於比較

public class Customer { 
int Age; 
public Customer(int age, String surname, String forename) { 
    super(); 
    Age = age; 
    Surname = surname; 
    Forename = forename; 
} 


String Surname,Forename; 

public int getAge() { 
    // TODO Auto-generated method stub 
    return Age; 
} 

public String getSurname() { 
    // TODO Auto-generated method stub 
    return Surname; 
} 


public String getForename() { 
    // TODO Auto-generated method stub 
    return Surname; 
} 



public void display() 
{ 
    // System.out.println(Forename+"\t"+Surname+"\t"+Age); 
    System.out.println(Age+"\t"+Forename+"\t"+Surname); 
    } 

}

,這裏是我的集合類..

class testCustomerComparator 
{ 

    public static void main(String... a) 
    { 

      Customer customerFirst = new Customer(46,"Alabama", "Christonson"); 
     Customer customerSecond = new Customer(21, "Anna", "Sobek"); 
     Customer customerThird = new Customer(27, "Rafael", "Sobek"); 

     List<Customer> list = new ArrayList<Customer>(); 
     list.add(customerThird); 
     list.add(customerSecond); 
     list.add(customerFirst); 
} 
} 

請告訴我如何爲這個班級製作課程,我想製作一個比較表,以便客戶列表按年齡排序,其次是姓氏排序。之後,你想按名排序。請指教我有嵌套條件裏面比較

邏輯必須是這樣的......

public class CustomerComparator implements Comparator<Customer> { 

    @Override 
    public int compare(Customer c1, Customer c2) { 

    if (c1.getAge() == c2.getAge()) { 
     if (c1.getSurname().compareTo(c2.getSurname()) == 0) { 
      return c1.getForename().compareTo(c2.getForename()) { 
     } else { 
      return c1.getSurname().compareTo(c2.getSurname()); 
     } 
    } else if (c1.getAge() > b2.getAge()) { 
     return -1; 
    } else { 
     return 1; 
    } 
    } 

,但它不能正常工作,請告知

回答

1

看起來很像功課。我可以給你一些提示,看看在哪裏。

你有兩個選擇:

  • 使POJO類擴展Comparable<Customer>
  • 定義自定義外部比較爲Comparator<Customer>

假設第二選擇,其中有兩個明確的客戶,你必須定義類似這樣的一種方法:使用

@Override 
public int compare(Customer c1, Customer c2) 
{ 
    // this method should return 0 if c1.equals(c2), 
    // should instead return 1 if c1 should come first than c2 and -1 otherwise 
} 
+0

如果您使用的是Java 1.6以上,這可能是值得註釋與'@ Override'的'compare'方法,這樣你,如果你的方法沒有實現相應的方法簽名會得到一個編譯時錯誤。 – oconnor0

+0

是,編輯,謝謝 – Jack

+0

oconnor0:你已經把1和-1混在一起了。合同中說:「第一個參數小於,等於或大於第二個參數時,爲負整數,零或正整數。」值得注意的是,「負整數」和「正整數」,而不是完全-1和1.我想這是爲了方便比較數值(簡單的減法,而不是一個大的if語句) – Matt

0
public class CustomerComparator implements Comparator<Customer> { 

public int compare(Customer c1, Customer c2) { 
.... here you have c1 and c2. compare returns -1 if c1 should go before c2, 
0 if they are found to be equal, and 1 if c2 should go before c1. 
You add the logic to compare c1 and c2 fields as you stated and return the result. 
} 

} 

然後Collections.sort排序該列表使用此比較器。

+0

請你可以通過製作比較級的代碼條款來顯示它 – user1380194

0

你可以幫助下面的代碼。

import java.util.*; 

class Customer { 

    private int age; 
    private String name; 
    private String forename; 

    public Customer(int age, String surname, String forename) { 
     super(); 
     this.age = age; 
     this.name = surname; 
     this.forename = forename; 
    } 

    public void setAge(int age) { 
     this.age = age; 
    } 

    public int getAge() { 
     return this.age; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public String getName() { 
     return this.name; 
    } 

    public void setForename(String forename) { 
     this.forename = forename; 
    } 

    public String getForename() { 
     return forename; 
    } 
} 

class AgeComparator implements Comparator { 

    public int compare(Object emp1, Object emp2) { 

     int emp1Age = ((Customer) emp1).getAge(); 
     int emp2Age = ((Customer) emp2).getAge(); 

     if (emp1Age > emp2Age) 
      return 1; 
     else if (emp1Age < emp2Age) 
      return -1; 
     else 
      return 0; 
    } 

} 

/* 
* The below given comparator compares employees on the basis of their name. 
*/ 

class NameComparator implements Comparator { 

    public int compare(Object emp1, Object emp2) { 

     // parameter are of type Object, so we have to downcast it to Employee 
     // objects 

     int emp1Age = ((Customer) emp1).getAge(); 
     int emp2Age = ((Customer) emp2).getAge(); 

     if (emp1Age > emp2Age) { 
      return 1; 
     } else if (emp1Age < emp2Age) { 
      String emp1Name = ((Customer) emp1).getName(); 
      String emp2Name = ((Customer) emp2).getName(); 

      // uses compareTo method of String class to compare names of the 
      // employee 
      return emp1Name.compareTo(emp2Name); 
     } else { 
      return 0; 
     } 
    } 

} 

class CustomerComparator implements Comparator { 

    public int compare(Object emp1, Object emp2) { 

     // parameter are of type Object, so we have to downcast it to Employee 
     // objects 

     String emp1Name = ((Customer) emp1).getName(); 
     String emp2Name = ((Customer) emp2).getName(); 

     // uses compareTo method of String class to compare names of the 
     // employee 
     return emp1Name.compareTo(emp2Name); 

    } 

} 

public class JavaComparatorExample { 

    public static void main(String args[]) { 

     // Employee array which will hold employees 
     Customer employee[] = new Customer[3]; 

     // set different attributes of the individual employee. 
     employee[0] = new Customer(46, "Alabama", "Christonson"); 
     employee[1] = new Customer(21, "Anna", "Sobek"); 
     employee[2] = new Customer(27, "Rafael", "Sobek"); 

     System.out.println("Order of employee before sorting is"); 
     // print array as is. 
     for (int i = 0; i < employee.length; i++) { 
      System.out.println("Employee " + (i + 1) + " name :: " 
        + employee[i].getName() + ", Age :: " 
        + employee[i].getAge()); 
     } 

     Arrays.sort(employee, new AgeComparator()); 
     System.out 
       .println("\n\nOrder of employee after sorting by employee age is"); 

     for (int i = 0; i < employee.length; i++) { 
      System.out.println("Employee " + (i + 1) + " name :: " 
        + employee[i].getName() + ", Age :: " 
        + employee[i].getAge()); 
     } 

     // Sorting array on the basis of employee Name by passing NameComparator 
     Arrays.sort(employee, new NameComparator()); 

     System.out 
       .println("\n\nOrder of employee after sorting by employee name is"); 
     for (int i = 0; i < employee.length; i++) { 
      System.out.println("Employee " + (i + 1) + " name :: " 
        + employee[i].getName() + ", Age :: " 
        + employee[i].getAge()); 
     } 

    } 

} 

希望這會幫助你。

編輯

看那CustomerComparator類。

+0

你還沒有理解我的查詢 – user1380194

+0

哦不。那麼什麼是問題?我開發代碼時無法理解。 –

+0

我只想要一個比較器而不是兩個分開的比較器Man – user1380194

0
@Override 
public int compare(Customer c1, Customer c2) { 
    int r = Integer.valueOf(c1.getAge()).compareTo(c2.getAge()); 
    if (r != 0) return r; 
    r = c1.getSurname().compareTo(c2.getSurname()); 
    if (r != 0) return r; 
    return c1.getForename().compareTo(c2.getForename()); 
}