从对的优先级队列中删除特定元素

-2 java priority-queue

PriorityQueue<Pair<Integer, Integer>> p = new PriorityQueue<>((a,b)->a.getValue()-b.getValue());
Run Code Online (Sandbox Code Playgroud)

这就是优先级队列的定义方式,其中您可以看到元素是根据键值对中的值而不是键进行排序的。现在我想删除一个特定的元素(不在队列顶部),使用键作为搜索因子。假设队列有元素 ->

p.add(new Pair<>(2,1));
p.add(new Pair<>(3,4));
p.add(new Pair<>(1,5);
Run Code Online (Sandbox Code Playgroud)

我想使用键 (3) 删除元素 (3,4);

删除后的预期输出应该是 -> [[2,1], [1,5]]

hev*_*ev1 5

你可以使用removeIf.

p.removeIf(x -> x.getKey() == 3);
// removes all elements with key equal to 3
Run Code Online (Sandbox Code Playgroud)