【发布时间】:2015-10-10 17:14:33
【问题描述】:
我知道很多答案已经回答了我的问题。在我的代码中,异常说“比较方法违反了其一般合同”,但我不知道我的比较方法如何违反其一般合同。这是我的代码:
public static List<Entry<Integer, Double>> sortMap(
Map<Integer, Double> curMap, final boolean isDesc) {
List<Entry<Integer, Double>> res = new ArrayList<Entry<Integer, Double>>();
for (Entry<Integer, Double> iter : curMap.entrySet()) {
res.add(iter);
}
Collections.sort(res, new Comparator<Entry<Integer, Double>>() {
public int compare(Entry<Integer, Double> o1,
Entry<Integer, Double> o2) {
if (o1.getValue() == o2.getValue()) {
return 0;
} else if (o1.getValue() > o2.getValue()) {
return isDesc ? -1 : 1;
}
return isDesc ? 1 : -1;
}
});
return res;
}
【问题讨论】:
标签: java comparison comparator