Updates
  • Starting New Weekday Batch for Java Full Stack Developer on 24th June 2024 @ 02:00 PM to 04:00 PM
  • Starting New Weekend Batch for Java Full Stack Developer on 06th July 2024 @ 11:00 AM to 02:00 PM
  • Starting New Weekday Batch for Java Full Stack Developer on 08th July 2024 @ 04:00 PM to 06:00 PM
  • Starting New Weekday Batch for Java Full Stack Developer on 29th July 2024 @ 10:00 AM to 12:00 PM
Join Course

PriorityQueue

PriorityQueue is a class available in the java.util package. It extends the java.util.AbstractQueue class and implements the java.io.Serializable interface.

Important point of PriorityQueue :

1. PriorityQueue is a data structure that represents a group of individual objects before processing according to some priority.
2. The priority order can be either the default natural sorting order or a customized sorting order specified by a Comparator object.
3. If depending on the default natural sorting order, the objects must be homogeneous and Comparable, or else a ClassCastException will occur.
4. If defining a customized sorting order with a Comparator, the objects need not be homogeneous and Comparable.
5. Insertion order is not preserved, but all objects will be inserted according to some priority.
6. null is not allowed, even as the first element for an empty PriorityQueue. Otherwise, a NullPointerException will occur.

            
import java.util.PriorityQueue;
publicclass JTC {
   publicstaticvoid main(String[] args) {
      PriorityQueuepriorityQueue = newPriorityQueue();
      priorityQueue.add(101);
      priorityQueue.add(34);
      priorityQueue.add(67);
      priorityQueue.add(-90);
      // priorityQueue.add("Hello"); :- java.lang.ClassCastException
      priorityQueue.add(101);
      System.out.println("priotityQueue :- " + priorityQueue);
   }
}
    priotityQueue :- [-90, 34, 67, 101, 101]
            

In this example, as we can see, we create an object of java.util.PriorityQueue or a PriorityQueue and add elements to it using the add() method of java.util.PriorityQueue.
As discussed, PriorityQueue enables us to store data based on some priority, which can be either natural sorting or a custom comparator.
In this example, we are working with the natural sorting of the java.util.Integer class, which follows ascending order.

            
import java.util.Comparator;
import java.util.Iterator;
import java.util.PriorityQueue;
class Student {
   intsid;
   String sname;
   String scity;
   floatmarks;
   public Student(intsid, String sname, String scity, floatmarks) {
      super();
      this.sid = sid;
      this.sname = sname;
      this.scity = scity;
      this.marks = marks;
   }
   public String toString() {
      return "Student [sid=" + sid + ", sname=" + sname + ", scity=" + scity + ", marks=" + marks + "]";
   }
}
class StudentMarksDescendingComparator implementsComparator {
   publicint compare(Object o1, Object o2) {
      Student s1 = (Student) o1;
      Student s2 = (Student) o2;
      if (s1.marks > s2.marks) {
         return -1;
      }
      elseif(s1.marks == s2.marks) {
         return 0;
      } else {
         return 1;
      }
   }
}
publicclass JTC {
   publicstaticvoid main(String[] args) {
      PriorityQueuepriorityQueue = new PriorityQueue(new StudentMarksDescendingComparator());
      priorityQueue.add(new Student(101, "Vivek", "Noida", 45));
      priorityQueue.add(new Student(102, "Amit", "Delhi", 90));
      priorityQueue.add(new Student(103, "Rahul", "Nagpur", 562));
      priorityQueue.add(new Student(104, "Neha", "Noida", 89));
      Iteratoriterator = priorityQueue.iterator();
      while (iterator.hasNext()) {
         System.out.println(iterator.next());
      }
   }
}
    Student [sid=103, sname=Rahul, scity=Nagpur, marks=562.0]
    Student [sid=104, sname=Neha, scity=Noida, marks=89.0]
    Student [sid=102, sname=Amit, scity=Delhi, marks=90.0]
    Student [sid=101, sname=Vivek, scity=Noida, marks=45.0]
            

In this example, we are storing Student objects in a PriorityQueue in descending order based on student marks, utilizing a custom comparator.

Important Constructor of java.util.PriorityQueue:

1. public java.util.PriorityQueue(): It creates an empty PriorityQueue with the default initial capacity of 11, and the order of elements depends on the defined natural sorting.

2. public java.util.PriorityQueue(int): It creates an empty PriorityQueue with a specified capacity, and the order of elements depends on the defined natural sorting.
 It throws IllegalArgumentException when we pass Initial Capacity less than 1.

3. public java.util.PriorityQueue(java.util.Comparator): It constructs an empty PriorityQueue with the default initial capacity of 11, and the order of elements is determined by the specified comparator.

4. public java.util.PriorityQueue(int, java.util.Comparator): It creates an empty PriorityQueue with a specified initial capacity, and the order of elements depends on the specified comparator, following the defined natural sorting.
It throws an IllegalArgumentException when an initial capacity less than 1 is passed.

5. public java.util.PriorityQueue(java.util.Collection): It creates a PriorityQueue that contains all the elements of the specified collection. An important point to note is that if the specified collection is of type SortedSet or PriorityQueue, the order of the elements will be the same; otherwise, it stores the elements based on their defined natural sorting.
It throws a ClassCastException when the elements of the specified collection cannot be compared to each other according to the priority queue ordering.
It throws a NullPointerException when the specified collection or any element of the given collection is null.

6. public java.util.PriorityQueue(java.util.PriorityQueue): It creates a PriorityQueue that contains all the elements of the specified PriorityQueue, and the order of the elements will be the same as in the given PriorityQueue.
It throws a ClassCastException when the elements of the specified PriorityQueue cannot be compared to each other according to the given priority queue ordering.
It throws a NullPointerException when the specified priority queue or any element of the given priority queue is null.

7. public java.util.PriorityQueue(java.util.SortedSet): It creates a PriorityQueue that contains all the elements of the specified SortedSet, and the order of the elements will be the same as in the given SortedSet.
It throws a ClassCastException when the elements of the specified SortedSet cannot be compared to each other according to the given sorted set ordering.
It throws a NullPointerException when the specified sorted set or any element of the given sorted set is null.