Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is PriorityQueue a FIFO Queue?

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

like image 692
ytz Avatar asked Dec 05 '22 14:12

ytz


2 Answers

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.

like image 164
ElderMael Avatar answered Dec 07 '22 02:12

ElderMael


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

like image 40
kosa Avatar answered Dec 07 '22 03:12

kosa