2014-01-08 91 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