Java generics warnings on java.util.Collections

后端 未结 4 1781
生来不讨喜
生来不讨喜 2021-02-19 23:39

I have a method:

public List sortStuff(List toSort) {
    java.util.Collections.sort(toSort);

    return toSort;
}

T

4条回答
  •  庸人自扰
    2021-02-20 00:40

    Sorting Generic Collections

    There are two sorting functions defined in this class, shown below:

    public static > void sort(List list);
    
    public static  void sort(List list, Comparator c);
    

    Neither one of these is exactly easy on the eyes and both include the wildcard (?) operator in their definitions. The first version accepts a List only if T extends Comparable directly or a generic instantiation of Comparable which takes T or a superclass as a generic parameter. The second version takes a List and a Comparator instantiated with T or a supertype.

提交回复
热议问题