2012-05-07 55 views
0

我在下面,這使得使用比較發達..關於compartor條件

這是我的POJO ..

class Emp 
{ 
     String name,job; 
     int salary; 
     public Emp(String n,String j,int sal) 
     { 
     name=n; 
     job=j; 
     salary=sal; 
     } 
     public void display() 
     { 
     System.out.println(name+"\t"+job+"\t"+salary); 
     } 
     public boolean equals(Object o) 
     { 
      Emp p=(Emp)o; 
      return this.name.equals(p.name)&&this.job.equals(p.job) &&this.salary==p.salary; 
     } 
     public int hashCode() 
     { 
      return name.hashCode()+job.hashCode()+salary; 
     } 

} 

這是我的用戶定義的集合類集..

ArrayList list=new ArrayList(); 
      list.add(new Emp("Ram","Trainer",34000)); 
      list.add(new Emp("Sachin","Programmer",24000)); 
      list.add(new Emp("Priyanka","Manager",54000)); 
      list.add(1,new Emp("Ravi","Administrator",44000)); 
      list.add(new Emp("Anupam","Programmer",34000)); 
      list.add(new Emp("Sachin","Team Leader",54000)); 
      System.out.println("There are "+list.size()+" elements in the list."); 
      System.out.println("Content of list are : "); 
     ListIterator itr=list.listIterator(); 
      System.out.println("Sort Object according to Salary"); 
     Collections.sort(list,new SalaryComparator()); 
     System.out.println("Content of list are : "); 
      itr=list.listIterator(); 
      while(itr.hasNext()) 
      { 
      Emp e=(Emp)itr.next(); 
      e.display(); 
      } 
     } 

最後我的比較級

class SalaryComparator 
    implements Comparator 
{ 
    public int compare(Object paramObject1, Object paramObject2) 
    { 
    Emp localEmp1 = (Emp)paramObject1; 
    Emp localEmp2 = (Emp)paramObject2; 
    return localEmp1.salary - localEmp2.salary; 
    } 
} 

現在它是按照遞增的順序在工資的基礎上進行排序,但是請建議我如何在分類中實施邏輯,以便首先對工資的基礎進行排序然後在工作的基礎上,即第一個工資然後工作,請指教。

+2

使用IF塊。如果工資相等,則根據比較結果進行下一個比較並將結果返回爲-1,0或1。 –

+0

@ HovercraftFullOfEels ..可以請你詳細解釋一下,如果可能的話,然後編碼,這將使理解更清晰,非常感謝 – user1380194

+0

順便說一句,你應該真正閱讀泛型。您使用的原始類型自Java 1.5以來已被半棄用(大約8年)。 – yshavit

回答

1

您可以隨時在比較器功能中包含多個比較。在這種情況下:

class SalaryComparator 
    implements Comparator 
{ 
    public int compare(Object paramObject1, Object paramObject2) 
    { 
    Emp localEmp1 = (Emp)paramObject1; 
    Emp localEmp2 = (Emp)paramObject2; 
    int salaryDiff = localEmp1.salary - localEmp2.salary; 
    if (salaryDiff != 0) { 
     return salaryDiff; 
    } else { 
     if (localEmp1.job == null) { 
     if (localEmp2.job == null) { 
      return 0; 
     } else { 
      return 1; 
     } 
     } else { 
     if (localEmp2.job == null) { 
      return -1; 
     } else { 
      return localEmp1.job.compareTo(localEmp2.job); 
     } 
     } 
    } 
    } 
} 

空檢查的這種設置使的Emp實例,而不以字母排序的最後一份工作的價值。

+0

非常感謝,請詳細解釋,如果可能,那麼一點點的代碼將有助於掌握更多,非常感謝 – user1380194

+0

這看起來像只是一個勺子餵養。 @ user1380194:在請求「一點點代碼」之前,你認爲你可以先聽取我們的建議並親自嘗試一下嗎?我敢肯定,它不會傷害你。 –

0

您可以使用仿製藥,使你的代碼更安全類型:

import java.util.*; 

class Emp 
// as is 

// use Comparator <Emp> 
class SalaryComparator implements Comparator <Emp> 
{ 
    // 'Emp' as param type, no casting or isinstanceof inside 
    public int compare (Emp emp1, Emp emp2) 
    { 
     int diff = emp1.salary - emp2.salary; 
     if (diff != 0) return diff; 
     else return (emp1.job.compareTo (emp2.job)); 
    } 
} 

public class EmpTest 
{ 
    public static void main (final String args[]) 
    { 
     // how to use collections generically should be known by now: 
     ArrayList <Emp> list = new ArrayList <Emp>(); 
     list.add (new Emp ("Ram","Trainer", 34000)); 
     // ... 

     // position 3 - slightly more complicated here ... 
     ListIterator <Emp> itr = list.listIterator(); 
     // ... 
     while (itr.hasNext()) 
     { 
       // but simpler here: 
      Emp e= itr.next(); 
      e.display(); 
     } 
    // here you could use the simplified for-loop: 
    for (Emp e : list) 
    { 
     e.display(); 
    } 
    } 
}