我想使用包含每个项目值的地图对列表进行排序。
Map<Integer, Float> map = new HashMap<>();
List<Integer> list = new ArrayList<>();
map.put(0, 0.0f);
map.put(1, 5.0f);
map.put(2, 2.0f);
list = new ArrayList<>(map.keySet());
Collections.sort(list, new Comparator<Integer>() {
public int compare(Integer left, Integer right) {
Float leftCost = map.get(left);
Float rightCost = map.get(right);
return leftCost.compareTo(rightCost);
}
})
我希望订单是0,2,1
因为 的值1
高于2
。但是java不让我这样做。我收到以下错误:Cannot refer to a non-final variable map inside an inner class defined in a different method
我怎么能这样做呢?