00:00

Difference Between Priority Queue and Queue Implementation in Java

Last updated:

A normal queue and a priority queue offer the same three basic operations (add, remove, peek) and in Java even share the same Queue interface. What differs is which element remove returns: the oldest one in a normal queue, the most important one in a priority queue. This page explains that difference and shows a simple priority queue implementation in Java.

What is a Priority Queue in Data Structure?

A priority queue stores elements with a priority and always removes an element with the best priority first.

Characteristics of a priority queue:

  • Every element has a priority, given by its value or by a comparator.
  • Removal returns an element with the best priority.
  • Elements with equal priority have no guaranteed order; java.util.PriorityQueue breaks ties arbitrarily.

Assigning priority value in a priority queue

Usually the element's own value is its priority. In a max-priority queue the largest value is removed first; in a min-priority queue the smallest is removed first. Java's PriorityQueue is a min-priority queue by default; pass Collections.reverseOrder() or any other Comparator to choose a different priority.

Difference between Priority Queue and Normal Queue

The key difference is the removal rule: a normal queue is FIFO (First In, First Out), while a priority queue serves the highest-priority element first. Everything else in the table follows from that.

CharacteristicNormal QueuePriority Queue
Removal orderFIFO: the element that arrived first leaves first.By priority: the element with the best priority leaves first, whenever it arrived.
Equal elementsAlways leave in arrival order.No guaranteed order among equal priorities (Java breaks ties arbitrarily).
Typical structureCircular array or linked list with head and tail.Binary heap stored in an array.
InsertO(1)O(log n) with a heap
RemoveO(1)O(log n) with a heap
PeekO(1)O(1)
Java classesArrayDeque, LinkedList (through the Queue interface)PriorityQueue, PriorityBlockingQueue
Typical usesPrint jobs, request handling, breadth-first search.Task scheduling by urgency, Dijkstra's and Prim's algorithms, top-k problems.

Priority Queue Implementation

A priority queue can be built on an unsorted array, a sorted array, a linked list, a balanced binary search tree or a binary heap. Each choice makes a different operation cheap:

ImplementationInsertRemove bestPeek best
Unsorted arrayO(1)O(n)O(n)
Sorted arrayO(n)O(1)O(1)
Binary heap (used by java.util.PriorityQueue)O(log n)O(log n)O(1)

The binary heap is the standard choice. The example below uses a sorted array because it is easier to follow: the array is kept in descending order, so the smallest value, which has the highest priority in this min-priority queue, is always at the end and can be removed in O(1).

SortedArrayPriorityQueue.java
// A min-priority queue of int values: the smallest value has the highest priority.
// The array is kept sorted from largest (index 0) to smallest (index count - 1),
// so the highest-priority element is always at the end.
public class SortedArrayPriorityQueue {
  private final int[] items;
  private int count;

  public SortedArrayPriorityQueue(int capacity) {
    items = new int[capacity];
    count = 0;
  }

  public boolean isEmpty() {
    return count == 0;
  }

  public boolean isFull() {
    return count == items.length;
  }

  // Inserts a value in its sorted place: O(n), smaller values shift right
  public void insert(int value) {
    if (isFull()) {
      throw new IllegalStateException("Priority queue is full");
    }
    int i = count - 1;
    // Move every value smaller than the new one one place to the right
    while (i >= 0 && items[i] < value) {
      items[i + 1] = items[i];
      i--;
    }
    items[i + 1] = value;
    count++;
  }

  // Removes and returns the smallest value: O(1)
  public int remove() {
    if (isEmpty()) {
      throw new IllegalStateException("Priority queue is empty");
    }
    return items[--count];
  }

  // Returns the smallest value without removing it: O(1)
  public int peek() {
    if (isEmpty()) {
      throw new IllegalStateException("Priority queue is empty");
    }
    return items[count - 1];
  }

  public static void main(String[] args) {
    SortedArrayPriorityQueue queue = new SortedArrayPriorityQueue(5);
    queue.insert(25);
    queue.insert(55);
    queue.insert(45);
    queue.insert(35);
    queue.insert(15);

    if (queue.isFull()) {
      System.out.println("Queue is full.");
    }

    System.out.println("Element removed from the Priority Queue: " + queue.remove());
    System.out.println("Element at front: " + queue.peek());

    System.out.print("Remaining elements in priority order: ");
    while (!queue.isEmpty()) {
      System.out.print(queue.remove() + " ");
    }
    System.out.println();
  }
}
Output
Queue is full.
Element removed from the Priority Queue: 15
Element at front: 25
Remaining elements in priority order: 25 35 45 55