PriorityQueue in Java

A PriorityQueue is used when the objects are supposed to be processed based on the priority. It is known that a Queue follows the First-In-First-Out algorithm, but sometimes the elements of the queue are needed to be processed according to the priority, that’s when the PriorityQueue comes into play.

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

  1. PriorityQueue(): Creates a PriorityQueue with the default initial capacity (11) that orders its elements according to their natural ordering.

  2. A heap is a tree-based data structure and can be classified as a complete binary tree.

  3. A [(i-1)/2] Represents the parent node
    A[(2i)+1] Represents the left child node
    A[(2
    i)+2] Represents the right child node

  4. insert & remove: heapify