Java集合排序的绑定不匹配

您需要有关Java集合排序的帮助.它给了我这个错误:

Bound mismatch: The generic method sort(List<T>) of type Collections is not applicable for the arguments (List<WifiSSID>). 
The inferred type WifiSSID is not a valid substitute for the bounded parameter <T extends Comparable<? super T>>

我的代码如下:

public class WifiSSID {

    public String SSIS;
    public double id;
}


 public class ScanFilterWifiList {

    public ScanFilterWifiList(List<WifiSSID> wifiList) {
        Collections.sort(wifiList);
           //Collections.sort(wifiList, new SortSSIDByid()); tried this also.
    }
}
interface Comparator<WifiSSID>
{
    int compare(WifiSSID obj1, WifiSSID obj2);
}

class SortSSIDByid implements Comparator<WifiSSID>
{
    @Override
    public int compare(WifiSSID ssid1, WifiSSID ssid2) 
    {
        int value = 0; 
        if (ssid1.id > ssid2.id) 
            value = 1; 
        else if (ssid1.id < ssid2.id) 
            value = -1; 
        else if (ssid1.id == ssid2.id) 
            value = 0; 
        return value; 
     }
}

我做错什么了吗?

解决方法:

您无法对未实现Comparable接口的对象列表进行排序.或者,可以,但是必须向Collections.sort()方法提供一个Comparator.

考虑一下:Collections.sort()如何在不知道WifiSSID何时小于另一个WifiSSID的情况下对列表进行排序?

您要使用Collections.sort(wifiList,new SortSSIDByid());

编辑:

您定义了自己的专有Comparator接口,并在SortSSIDByid中实现了此专有Comparator接口. Collections.sort()需要一个java.util.Comparator实例.它不是您不知道的专有Comparator接口的实例.

上一篇:java-在Comparator.comparing中转换为可序列化


下一篇:数据结构之堆与Java实现