~/ learn/ comp-308/ cards/ Queue, Deque, PriorityQueue & Comparable/Comparator
1 of 5

PriorityQueue ordered by a Comparator

PriorityQueue ordered by a Comparator

Answer

PriorityQueue<Event> schedule = new PriorityQueue<>(Comparator.comparingLong(Event::getTime)); schedule.offer(new Bell(200)); Event soonest = schedule.poll();

The comparator orders events by their long time; the heap keeps the minimum at the head, so poll() returns the earliest-scheduled event regardless of insertion order. Comparator.comparingLong avoids boxing the long key.

space flip · ← → navigate · esc to exit
NORMAL ~/memra/library/77ba4984-c9cb-4e14-aa07-2d8cf7ed3b05/flashcard utf-8 LF