Each time you add anything in to a **PriorityQueue**, does it rearrange itself?

478 Views Asked by At

When you use a PriorityQueue without a Comparator, does the queue rearrange itself after each offer and if yes, how to avoid it?

1

There are 1 best solutions below

1
On

From the Javadoc :

An unbounded priority queue based on a priority heap. 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.

Yes the queue rearranges itself when elements are added in order to remain ordered. You can't avoid it. If you don't supply a Comparator, the natural ordering (defined by the Compabable implementation of the element type) is used. If that doesn't suit your purposes, don't use a PriorityQueue.