admin管理员组

文章数量:1130192

I have the following code snippet:

Map<Integer, Integer> map = new HashMap<>();
PriorityQueue<int[]> pq = new PriorityQueue<>((a, b) -> map.get(b[1]) == map.get(a[1]) ? b[2] - a[2] : map.get(b[1]) - map.get(a[1]));
List<List<Integer>> l = new ArrayList<>();
public TaskManager(List<List<Integer>> tasks) {
    for(List<Integer> t: tasks) {
        map.put(t.get(1), t.get(2));
        pq.add(new int[]{t.get(0), t.get(1), t.get(2)});
    }
}

In short, it's a priority queue the order of elements in which is based on the value of these elements in the hash map. When I run this code I get the following runtime error:

java.lang.NullPointerException: Cannot invoke "java.lang.Integer.intValue()" because the return value of "java.util.Map.get(Object)" is null

It seems like the priority queue cannot find the key in the map even though I am putting this key into the map in the line immediately before the one that adds elements into the priority queue. Could somebody please help me understand why this is? Thank you in advance!

本文标签: javaMap Value is Null when Defining PriorityQueue Based on the MapStack Overflow