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
版权声明:本文标题:java - Map Value is Null when Defining PriorityQueue Based on the Map - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736757375a1951362.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论