Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 366
Gergely Fehérvári Avatar asked Nov 30 '25 02:11

Gergely Fehérvári


1 Answers

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.

like image 133
Oskar Kjellin Avatar answered Dec 02 '25 14:12

Oskar Kjellin