2014-01-08 94 views
1

想象我有兩個POJO豆狀:Java的泛型方法調用在Java

public class Employee { 
    private String name; 
    private long id; 
    .... 
    //GETTERS AND SETTERS 
} 

public class Buildings { 
    private String name; 
    private long numOfEmployees; 
    ... 
    //GETTERS AND SETTERS 
} 

現在我做這些的POJO的兩個列表(empList & bldList),我想使用Collections.sort功能,這樣對它們進行排序:

Collections.sort(empList , new Comparator<Employee>() { 
     public int compare(Employee o1, Employee o2) { 
     return o2.getId().compareTo(o1.getId()); 
     } 
}); 

和另一個排序是這樣的:

Collections.sort(bldList, new Comparator<Buildings>() { 
     public int compare(Buildings o1, Buildings o2) { 
     return o2.getNumOfEmployees().compareTo(o1.getNumOfEmployees()); 
     } 
}); 
現在

代替鄰˚F寫比較兩次爲了這個,我在想,我發現我可以使用泛型和方法做這裏面操作的解決方案,有什麼來到我的腦海裏是一個類似的方法:

public <T> List<T> sortMyList(List<T> list){ 
    Collections.sort(list, new Comparator<T>() { 
     public int compare(T o1, T o2) { 
      // I don't know how to call the Building and Employee methods here so I just show it with "???" 
      return o1.???.compareTo(o2.???); 
      } 
    }); 
} 

我怎樣才能讓這種方法適合我嗎?

+1

問題的類都實現'Comparable'代替。 http://docs.oracle.com/javase/7/docs/api/java/util/Collections.html#sort(java.util.List)另請參閱:http://docs.oracle.com/javase/tutorial /collections/interfaces/order.html –

+0

[如何在Java中對對象數組進行排序?](http://stackoverflow.com/questions/18895915/how-to-sort-an-array-of-objects -in-java) –

回答

5

讓你的類實現Comparable<T>

class Employee implements Comparable<Employee>{ 

    //override compareTo() 

} 

類似地,對於Buildings

然後使用Collections.sort(listOfEmployee);

-1

另一種解決方案(比使用可比)

可以在Java中使用instanceof運算符。

例子:

public <T> List<T> sortMyList(List<T> list){ 
      Collections.sort(list, new Comparator<T>() { 
      public int compare(T o1, T o2) { 
       if(o1 instanceof Employee && o2 instanceof Employee) 
        return o2.getId().compareTo(o1.getId()); 
       else if(o1 instanceof Buildings && o2 instanceof Buildings) 
         return o2.getNumOfEmployees().compareTo(o1.getNumOfEmployees()); 
      } 
    }); 
} 
+1

我不會推薦這個,使用這種方式的instanceof是可怕的代碼風格。特別是因爲這是用界面優雅地解決的。 – Others

+0

是的,我明白了,但是如果員工和建築類別不可改變,那麼呢? –

+1

他說他寫了他們,爲什麼他們不能改變? – Others

0

它是實現Comparable是去正確的和最佳的方式普遍共識。

我的回答是「可以做到」而不是「最佳做法」,「我怎樣才能使這種方法適合我?」

這個答案應該被看作是學術(並且需要改進)而不是作爲可實施的解決方案。

public <T> void sortMyList(List<T> list) { 

    Collections.sort(list, new Comparator<T>() { 
     Method theMethod = null; 

     @Override 
     public int compare(T o1, T o2) { 
      if (theMethod == null) { 
       Method[] methods = o1.getClass().getMethods(); //reflection 
       for (Method m : methods) { 
        if ("long".equals(m.getReturnType().toString())) { 
         theMethod = m; 
         break; 
        } 
       } 
      } 
      try { 
       return ((Long) (theMethod.invoke(o1, null))).compareTo((Long) (theMethod.invoke(o2, null))); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
      return 0; 
     } 
    }); 
} 

這假設第一種方法,返回列表中找到一個long,用於比較。

循環得到Methodcompare(T o1, T o2)避免像getting the class of a generic type