PriorityQueue

  • If we want to represent a group of individual object prior to processing with priority then we should select PrirotyQueue
  • The priority can we either default natural sorting order or customized sorting order defined by comparator
  • Insertion order is not preserved and it is based on some priority
  • Duplicates objects are not allowed
  • If we depending on default natural sorting order compulasry the object should be Homogeneous and comparable else run time exception with class cast exception
  • Null is not allowed even as the first element

Constructors

  • Queue q = new PriorityQueue()
    • Creates queue with size 11 and Default natural sorting order
  • Queue q = new PriorityQueue(int initialCapacity)
    • Creates queue with customized size and Default natural sorting order
  • Queue q = new PriorityQueue(int initialCapacity, Comparator c)
    • Creates queue with customized size and with customized sorting order
  • Queue q = new PriorityQueue(SortedSet s)
  • Queue q = new PriorityQueue(Collection c)
    • Cross collection conversion

Example :

Note : The order is not as expected because the operating system won’t prioritize is properly.

Customized Priority Queue Example :

Leave a Comment