2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

優先度付きキュー(PriorityQueue) maxキューにする

Last updated at Posted at 2018-02-21

ちょっと忘れていたので、ここにメモしておく。

普通PriorityQueueはminimum ヒープです。

PriorityQueue pq= new PriorityQueue();
pq.poll(); //これは最小の要素を取得する

これを最大の値(maximum heap)から順に取得できるようにするには、

PriorityQueue pq = new PriorityQueue<>(Collections.reverseOrder());

とすればいい。

後、オブジェクトの特定のフィールドを使う場合は
PriorityQueue pq = new PriorityQueue(Comparator.comparing(SumTree::getAge));
もしくは
PriorityQueue pq = new PriorityQueue(Comparator.comparing(SumTree::getAge).reversed());

とすればいい。

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?