Normally, objects in Queue are placed in the FIFO order. i.e., First-in-First-Out. In certain cases, objects have to be processed based on their priority, and at that time, Java PriorityQueue came into action. In addition to that, PriorityQueue has certain features they are:
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
Declaration of Java PriorityQueue
Java PriorityQueue can be declared using the below syntax.
public class PriorityQueue<E> extends AbstractQueue<E> implements Serializable
Following are the constructors that are commonly used in Java PriorityQueue:
1. PriorityQueue(): A PriorityQueue will be created with 11 as its initial capacity is in default. Moreover, the elements are ordered based on natural ordering.
2. PriorityQueue(Collection extends E> c): A PriorityQueue will be created with elements in the mentioned collection.
3. PriorityQueue(int ic): A PriorityQueue will be created with the initial capacity ic mentioned. Moreover, the elements are ordered based on natural ordering.
4. PriorityQueue(int ic, Comparator super E> comparator): A PriorityQueue will be created with the initial capacity ic mentioned. Moreover, the elements are ordered based on the mentioned comparator.
5. PriorityQueue(PriorityQueue extends E> c): A PriorityQueue will be created with elements in the mentioned PriorityQueue.
6. PriorityQueue(SortedSet extends E> c): A PriorityQueue will be created with elements in the mentioned sorted set.
Now, let us see some of the commonly used methods in Java PriorityQueue:
1. add(E e): Element e will be added to the PriorityQueue on calling this method.
2. size(): Count of elements in the collection will be returned.
3. clear(): All the elements in the PriorityQueue will be removed.
4. comparator(): Comparator that is used to sort the queue will be returned. If natural ordering is used, null will be returned.
5. contains(Objecto): If the queue contains the mentioned element o, true will be returned.
6. iterator(): The iterator that is used over the elements in the queue will be returned.
7. offer(Ee): Mentioned element e will be inserted in the queue.
8. peek(): Head of the PriorityQueue will be retrieved, not removed. In case no elements are present, null will be returned.
9. poll(): Head of the PriorityQueue will be retrieved and removed. In case no elements are present, null will be returned.
10. remove(Objecto): A single instance of the mentioned element will be removed from the queue.
11. toArray(): An array of all the elements in the queue will be returned.
12. toArray(T[] a): An array of all the elements in the queue will be returned in which the runtime will be as that of the mentioned array.
Given below are the examples of Java PriorityQueue:
Sample program to create a PriorityQueue.
Code:
import java.util.Iterator; import java.util.PriorityQueue; class Main{ public static void main(String args[]){ //create a PriorityQueue PriorityQueue<String> q=new PriorityQueue<String>(); //add elements to the queue q.add("Anna"); q.add("Annamu"); q.add("Adam"); q.add("Iza"); q.add("Thukidi"); System.out.println("Head of the queue:"+q.element()); System.out.println("Head of the queue :"+q.peek()); //Retrieve elements in queue using iterator System.out.println("Queue elements are :"); Iterator it=q.iterator(); while(it.hasNext()) { System.out.println(it.next()); } //remove the element from queue q.remove(); //remove the head of the queue q.poll(); System.out.println("\n Queue after the removal of 2 elements :"); //Retrieve elements in queue using iterator Iterator<String> it2=q.iterator(); while(it2.hasNext()) { System.out.println(it2.next()); } } }
Output:
Working of the sample program:
Sample program to create a PriorityQueue using a comparator.
Code:
import java.util.Comparator; import java.util.Iterator; import java.util.PriorityQueue; class PriorityQueueExample{ public static void main(String[] args) { //Create a custom comparator. In this, length of 2 strings are getting compared Comparator<String> cmp = new Comparator<String>() { @Override public int compare(String str1, String str2) { return str1.length() - str2.length(); } }; // PriorityQueue creation with Comparator PriorityQueue<String> q = new PriorityQueue<>(cmp); // Add elements to the Queue q.add("Anna"); q.add("Annamu"); q.add("Adam"); q.add("Iza"); q.add("Thukidi"); q.add("Sam"); q.add("Elsa"); q.add("Kukku"); q.add("Mathu"); q.add("Radha"); // Remove elements from the Queue while (!q.isEmpty()) { System.ou<em>t</em>.println(q.remove()); } } }
Output:
Working of the sample program:
Sample program to implement a PriorityQueue by making use of different methods.
Code:
import java.util.*; class Main{ public static void main(String args[]){ //create a PriorityQueue PriorityQueue<String> q=new PriorityQueue<String>(); //add elements to the queue q.add("Anna"); q.add("Annamu"); q.add("Adam"); q.add("Iza"); q.add("Thukidi"); System.out.println("Head of the queue:"+q.element()); System.out.println("Head of the queue :"+q.peek()); //Retrieve elements in queue using iterator System.out.println("Queue elements are :"); Iterator it=q.iterator(); while(it.hasNext()) { System.out.println(it.next()); } //remove the element from queue q.remove(); //remove the head of the queue q.poll(); // Check whether the element Anna is present in queue using the method Contains() boolean b = q.contains("Anna"); System.out.println("Is there any element Anna in the PriorityQueue ? " + b); //Check whether the element Iza is present in queue using the method Contains() boolean bl = q.contains("Iza"); System.out.println("Is there any element Anna in the PriorityQueue ? " + bl); } }
Output:
Working of the sample program:
The above is the detailed content of PriorityQueue in Java. For more information, please follow other related articles on the PHP Chinese website!