【发布时间】:2015-12-19 15:21:56
【问题描述】:
我尝试排序,但出现编译错误
Collections类型中的方法sort(List<T>, Comparator<? super T>)不适用于参数(List<Map.Entry<T,Integer>>, HashMapHistogramComparator<T>)
这是我的代码:
public class HashMapHistogramIterator<T> implements Iterator<T> {
private List<HashMap.Entry<T,Integer>> items;
Iterator<HashMap.Entry<T,Integer>> lst;
public HashMapHistogramIterator(HashMapHistogram<T> hshMp) {
this.items = new ArrayList<HashMap.Entry<T,Integer>>();
this.items.addAll(hshMp.getAllMap());
Collections.sort(this.items, new HashMapHistogramComparator<T>(hshMp));
this.lst = this.items.iterator();
}
}
那是我的竞争对手:
import java.util.Comparator;
public class HashMapHistogramComparator<T> implements Comparator<T>{
HashMapHistogram<T> hshMp;
public HashMapHistogramComparator(HashMapHistogram<T> hshMp){
this.hshMp = hshMp;
}
@Override
public int compare(T arg0, T arg1) {
int val1 = this.hshMp.getValue(arg0);
int val2 = this.hshMp.getValue(arg1);
return Integer.compare(val2,val1);
}
}
【问题讨论】:
-
如果你想排序
HashMap.Entry,那么你的比较器应该是class HashMapHistogramComparator<T> implements Comparator<HashMap.Entry<T,Integer>>。
标签: java sorting comparator