Bound mismatch for java Collections sorting

后端 未结 2 1615
春和景丽
春和景丽 2021-01-17 01:39

Hi need Help regarding java collection sorting. It gives me this error:

Bound mismatch: The generic method sort(List) of type Collections is not app         


        
相关标签:
2条回答
  • 2021-01-17 02:13

    You can't sort a List of objects that don't implement the Comparable interface. Or rather, you can, but you have to provide a Comparator to the Collections.sort() method.

    Think about it: how would Collections.sort() sort your list without knowing when a WifiSSID is smaller or bigger than another one?

    You want to use Collections.sort(wifiList, new SortSSIDByid());

    EDIT:

    You defined your own proprietary Comparator interface, and implement this proprietary Comparator interface in SortSSIDByid. Collections.sort() wants an intance of java.util.Comparator. Not an instance of your proprietary Comparator interface, that it doesn't know.

    0 讨论(0)
  • 2021-01-17 02:22

    Just add this import import java.util.Comparator;

    and remove this interface

    interface Comparator<WifiSSID>
    {
        int compare(WifiSSID obj1, WifiSSID obj2);
    }
    

    Your SortSSIDByid comparator class will now implement java.util.Comparator and that is what is required by the Collections.sort() method.

    0 讨论(0)
提交回复
热议问题