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.