Is PriorityQueue a FIFO Queue?
Asked Answered
M

6

5

PriorityQueue implements Queue, but is PriorityQueue a FIFO data structure like Queue?

Mahalia answered 2/10, 2012 at 14:40 Comment(1)
it is priority based, not order basedTejeda
U
7

From the Queue interface:

Queues typically, but do not necessarily, order elements in a FIFO (first-in-first-out) manner. Among the exceptions are priority queues, which order elements according to a supplied comparator, or the elements' natural ordering

So PriorityQueue is an exception and it becomes a FIFO queue only if the comparator sorts in that order.

Uttica answered 2/10, 2012 at 14:43 Comment(0)
S
6

No, it is not. As per Javadoc

The elements of the priority queue are ordered according to their natural ordering, or by a Comparator provided at queue construction time, depending on which constructor is used

AND

The head of this queue is the least element with respect to the specified ordering

Surge answered 2/10, 2012 at 14:42 Comment(1)
+1 notionally the ordering could be FIFO, but it is usually used for something more interesting.Yeaton
S
4

PriorityQueue does not care about FIFO / LIFO. it handles priority. in case of several objects with same priority - you can't count on any of FIFO LIFO behavior.

Stonyhearted answered 2/10, 2012 at 14:44 Comment(0)
W
2

A priority queue is a data structure which keeps elements in a consistent internal order - in the Java implementation this ordering is specified at construction time. The head of the queue, and the order of the other elements is determined by the ordering criteria you specify.

For example, say you have a PriorityQueue containing students, and the ordering you set is ascending age - the head of the queue will contain the youngest student and the tail will be the oldest. As you add to the PriorityQueue, student will be inserted in the correct position according to their age, as that is the ordering you specified, the order of their insertion is irrelevant.

Wharf answered 2/10, 2012 at 14:49 Comment(0)
C
0

Queues in Java does not necessarily follow FIFO (First-In First Out) principle. Refer here: https://docs.oracle.com/javase/9/docs/api/java/util/Queue.html

Queues typically, but do not necessarily, order elements in a FIFO (first-in-first-out) manner. Among the exceptions are priority queues, which order elements according to a supplied comparator, or the elements' natural ordering.

PriorityQueue does not follow FIFO principle which must be the reason for your problem

Chesson answered 21/5, 2022 at 9:36 Comment(0)
A
0

It hinges on the Comparator you use.

For instance, say you're using a Priority Queue to manage an Event Queue of events scheduled to be called in the future, whether in wall-clock future or in a notional batch-mode future. The highest priority is basically the earliest time, and you'd use a comparator such as:

first.time < second.time

If the Comparator simply compares the time of two events, they will be dequeued in an undefined order and most likely not FIFO.

However, you can maintain a count of your Enqueue operations; store that in the priority queue elements; and make your comparison use the queue operation as a tie-breaker. Example:

first.time < second.time || ( first.time == second.time && first.count < second.count )

Then you will get FIFO behavior. If the counter is 32 bit, you can do 1000 enqueues/sec for 1000 hours before it wraps around. If the counter is 64-bit, you can do a billion enqueues/sec for 500+ years before it wraps. Even when the counter wraps, you can simply renumber all existing Priority Queue elements: deque them all into a vector; sort the vector; overwrite each vector element's counter with its vector element number; and reinsert to the priority queue. That vector size becomes your new counter.

Allele answered 22/1, 2023 at 19:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.