Search code examples
javalistsortingcollectionsfinal

Java comparator uses non final variables


I want to sort a list, using a map which contains the values for each item.

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);
    }
})

I want the order to be 0,2,1 because the value of 1 is higher than 2. But java dont let me do this. I get the following error: Cannot refer to a non-final variable map inside an inner class defined in a different method

How can I do this a way like that?


Solution

  • Just make it final:

    final Map<Integer, Float> map = new HashMap<Integer, Float>();
    List<Integer> list = new ArrayList<Integer>(); // this assignment is unncessary [1]
    
    map.put(0, 0.0f);
    map.put(1, 5.0f);
    map.put(2, 2.0f);
    
    list = new ArrayList<Integer>(map.keySet()); // 1. assignment is replaced here
    
    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);
        }
    })
    

    As your map is mutable, you can still modify it.