~/ 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/257ac40e-af8a-4922-9231-c5f66ad7d825/flashcard utf-8 LF