Java generics warnings on java.util.Collections

后端 未结 4 1776
生来不讨喜
生来不讨喜 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:33

    Collections.sort(List) expects that T must implement Comparable. It seems like Stuff does implement Comparable but doesn't provide the generic type argument.

    Make sure to declare this:

    public class Stuff implements Comparable
    

    Instead of this:

    public class Stuff implements Comparable
    

提交回复
热议问题