PriorityQueue and its methods
Before discussing about PriorityQueue let us look at the Collection Hierarchy.
Collection Hierarchy
The below diagram shows the Collection hierarchy consisting of classes and interfaces.
What is PriorityQueue?
PriorityQueue is a class which stores all the Collection elements. It is the child class of Queue interface, grandchild class of Collection interface and great grand child class of Iterable interface. The keyword used to create relationship between PriorityQueue and Queue, PriorityQueue and Collection and PriorityQueue and Iterable is “extends”.It is not thread safe and not synchronized as a result it can be used in multithreading operation with the help of PriorityBlockingQueue class which is the implemented class of BlockingQueue interface.PriorityQueue does not allow null values and we cannot create Objects of PriorityQueue that are non comparable.Although in Queue elements are added and removed in First In First Out (FIFO) manner but in some Queues FIFO is not followed. PriorityQueue is one of such Queues where FIFO is not followed for adding and removing of Collection elements but instead elements are added or removed based on Priority. In other words the element will highest ASCII value will have highest priority for adding into the Collection or removing from the Collection. The PriorityQueue elements are arranged in an orderly way with the help of a Comparator provided during time of Queue construction.
Methods of PriorityQueue class
Method Name | Description |
add(Object e) | This method is used to add a new element into the PriorityQueue class. |
addAll(Collection c) | This method is used to add all elements into the PriorityQueue class. |
clear() | It is used to clear all the elements from the PriorityQueue class but it does not clear the PriorityQueue class. |
contains(Object o) | This method is used to check the presence of a Collection element in the PriorityQueue. It returns ‘true’ value if the element is present in PriorityQueue and ‘false’ value if the element is not present in the PriorityQueue. |
containsAll(Collection c) | This method is used to check if the PriorityQueue contains all the collection elements. It returns ‘true’ value if all the elements are present inPriorityQueue and ‘false’ value if any one of the element is not present in the PriorityQueue. |
isEmpty() | This method checks if the given PriorityQueue is empty |
iterator() | This method is used to iterate sequentially all the Collection elements present in a PriorityQueue. |
remove(Object o) | This method is used to remove a specified collection element from the PriorityQueue. |
removeAll(Collection c) | This method removes all those Collection elements from the current PriorityQueue which are present in another PriorityQueue. |
retainAll(Collection c) | This method retains all those elements from the current PriorityQueue which are present in another PriorityQueue and removes those elements which are present in current PriorityQueue but not in another PriorityQueue and vice versa. |
size() | This method is used to find out and display the number of Collection elements present in the PriorityQueue. |
toArray() | This method is used to convert all collection elements present in a PriorityQueue into an array. |
toString() | This method is used to convert all collection elements present in a PriorityQueue into a String. |
offer(Object e) | This method is used to add new elements into the PriorityQueue class. |
poll() | This method is used to remove the topmost element of the PriorityQueue class which is displayed in output console. |
peek() | This method is used to fetch or point out the topmost element of the PriorityQueue which is displayed in output console but does not remove that element from the PriorityQueue class. |
element() | This method is used to fetch or point out the topmost element of the PriorityQueue which is displayed in output console but does not remove that element from the PriorityQueue class. |
1.Let us check how addAll(Collection c) works in PriorityQueue class.
Code Snippet:
package priorityQueueClass;
import java.util.PriorityQueue;
public class PriorityQueueClass {
public static void main(String[] args) {
PriorityQueue a1= new PriorityQueue();
a1.add("Italy");
a1.add("France");
a1.add("Germany");
a1.add("Russia");
a1.add("Australia");
a1.add("England");
PriorityQueue a2= new PriorityQueue();
a2.addAll(a1);
System.out.println(a2);
}
}
Output: [Australia, France, England, Russia, Italy, Germany]
Screenshot:
2. Let us check how clear() method works in Queue interface.
Code Snippet:
package priorityQueueClass;
import java.util.PriorityQueue;
public class PriorityQueueClass {
public static void main(String[] args) {
PriorityQueue a1= new PriorityQueue();
a1.add("Italy");
a1.add("France");
a1.add("Germany");
a1.add("Russia");
a1.add("Australia");
a1.add("England");
a1.clear();
System.out.println(a1);
}
}
Output: []
Screenshot:
3.Let us check how contains(Object o) method works in PriorityQueue class.
Code Snippet:
package priorityQueueClass;
import java.util.PriorityQueue;
public class PriorityQueueClass {
public static void main(String[] args) {
PriorityQueue a1= new PriorityQueue();
a1.add("Italy");
a1.add("France");
a1.add("Germany");
a1.add("Russia");
a1.add("Australia");
a1.add("England");
System.out.println("The given priorityqueue contains India: "+a1.contains("India"));
System.out.println("The given priorityqueue contains Australia: "+a1.contains("Australia"));
}
}
Output: The given priorityqueue contains India: false
The given priorityqueue contains Australia: true
Screenshot:
4. Let us check how containsAll(Collection c) method works in PriorityQueue class.
Code Snippet:
package priorityQueueClass;
import java.util.PriorityQueue;
public class PriorityQueueClass {
public static void main(String[] args) {
PriorityQueue a1= new PriorityQueue();
a1.add("Italy");
a1.add("France");
a1.add("Germany");
a1.add("Russia");
a1.add("Australia");
a1.add("England");
PriorityQueue a2= new PriorityQueue();
a2.add("Italy");
a2.add("France");
a2.add("Germany");
a2.add("Russia");
System.out.println("Does a2 contains all elements of a1:"+a2.containsAll(a1));
System.out.println("Does a1 contains all elements of a2:"+a1.containsAll(a2));
}
}
Output: Does a2 contains all elements of a1:false
Does a1 contains all elements of a2:true
Screenshot:
5.Let us check how isEmpty() method works in PriorityQueue class.
Code Snippet:
package priorityQueueClass;
import java.util.PriorityQueue;
public class PriorityQueueClass {
public static void main(String[] args) {
PriorityQueue a1= new PriorityQueue();
a1.add("Italy");
a1.add("France");
a1.add("Germany");
a1.add("Russia");
a1.add("Australia");
a1.add("England");
PriorityQueue a2= new PriorityQueue();
System.out.println("Is a1 PriorityQueue empty?: "+a1.isEmpty());
System.out.println("Is a2 PriorityQueue empty?: "+a2.isEmpty());
}
}
Output: Is a1 PriorityQueue empty?: false
Is a2 PriorityQueue empty?: true
Screenshot:
6. Let us check how iterator() method works in PriorityQueue class.
Code Snippet:
package priorityQueueClass;
import java.util.Iterator;
import java.util.PriorityQueue;
public class PriorityQueueClass {
public static void main(String[] args) {
PriorityQueue a1= new PriorityQueue();
a1.add("Italy");
a1.add("France");
a1.add("Germany");
a1.add("Russia");
a1.add("Australia");
a1.add("England");
Iterator a2= a1.iterator();
System.out.println("The collection elements values are: ");
while(a2.hasNext())
{
System.out.println(a2.next());
}
}
}
Output: The collection elements values are:
Australia
France
England
Russia
Italy
Germany
Screenshot:
7. Let us check how remove(Object o) method works in PriorityQueue class.
Code Snippet:
package priorityQueueClass;
import java.util.PriorityQueue;
public class PriorityQueueClass {
public static void main(String[] args) {
PriorityQueue a1= new PriorityQueue();
a1.add("Italy");
a1.add("France");
a1.add("Germany");
a1.add("Russia");
a1.add("Australia");
a1.add("England");
a1.remove("Italy");
a1.remove("France");
System.out.println(a1);
}
}
Output: [Australia, Germany, England, Russia]
Screenshot:
8. Let us check how removeAll(Collection c) works in PriorityQueue class.
Code Snippet:
package priorityQueueClass;
import java.util.PriorityQueue;
public class PriorityQueueClass {
public static void main(String[] args) {
PriorityQueue a1= new PriorityQueue();
a1.add("Italy");
a1.add("France");
a1.add("Germany");
a1.add("Russia");
a1.add("Australia");
a1.add("England");
PriorityQueue a2= new PriorityQueue();
a2.add("Italy");
a2.add("France");
a2.add("Germany");
a1.removeAll(a2);
System.out.println("Elements present in a1 after removing a2 elements from a1 are:"+a1);
}
}
Output: Elements present in a1 after removing a2 elements from a1 are:[Australia, England, Russia]
Screenshot:
9. Let us check how retainAll(Collection c) works in PriorityQueue class.
Code Snippet:
package priorityQueueClass;
import java.util.PriorityQueue;
public class PriorityQueueClass {
public static void main(String[] args) {
PriorityQueue a1= new PriorityQueue();
a1.add("Italy");
a1.add("France");
a1.add("Germany");
a1.add("Russia");
a1.add("Australia");
a1.add("England");
PriorityQueue a2= new PriorityQueue();
a2.add("Italy");
a2.add("France");
a2.add("Germany");
a1.retainAll(a2);
System.out.println("Elements present in a1 after retaining a2 elements are:"+a1);
}
}
Output: Elements present in a1 after retaining a2 elements are:[France, Italy, Germany]
Screenshot:
10. Let us check how size() method works in Queue interface.
Code Snippet:
package priorityQueueClass;
import java.util.PriorityQueue;
public class PriorityQueueClass {
public static void main(String[] args) {
PriorityQueue a1= new PriorityQueue();
a1.add("Italy");
a1.add("France");
a1.add("Germany");
a1.add("Russia");
a1.add("Australia");
a1.add("England");
System.out.println("The size of the collection elements present in the PriorityQueue is: "+a1.size());
}
}
Output: The size of the collection elements present in the PriorityQueue is: 6
Screenshot:
11. Let us check how toArray() method works in PriorityQueue class.
Code Snippet:
package priorityQueueClass;
import java.util.PriorityQueue;
public class PriorityQueueClass {
public static void main(String[] args) {
PriorityQueue a1= new PriorityQueue();
a1.add("Italy");
a1.add("France");
a1.add("Germany");
a1.add("Russia");
a1.add("Australia");
a1.add("England");
Object a2[]= a1.toArray();
System.out.println("The list of array elements are:");
for(int i=0;i<=a2.length-1;i++)
{
System.out.println(a2[i]);
}
}
}
Output: The list of array elements are:
Australia
France
England
Russia
Italy
Germany
Screenshot:
12. Let us check how toString() method works in PriorityQueue class.
Code Snippet:
package priorityQueueClass;
import java.util.PriorityQueue;
public class PriorityQueueClass {
public static void main(String[] args) {
PriorityQueue a1= new PriorityQueue();
a1.add("Italy");
a1.add("France");
a1.add("Germany");
a1.add("Russia");
a1.add("Australia");
a1.add("England");
System.out.println(a1.toString());
}
}
Output: [Australia, France, England, Russia, Italy, Germany]
Screenshot:
13. Let us check how offer(Object e) method works in PriorityQueue class.
Code Snippet:
package priorityQueueClass;
import java.util.PriorityQueue;
public class PriorityQueueClass {
public static void main(String[] args) {
PriorityQueue a1= new PriorityQueue();
a1.offer("Italy");
a1.offer("France");
a1.offer("Germany");
a1.offer("Russia");
a1.offer("Australia");
a1.offer("England");
System.out.println(a1);
}
}
Output: [Australia, France, England, Russia, Italy, Germany]
Screenshot:
14. Let us check how poll() method works in PriorityQueue class.
Code Snippet:
package priorityQueueClass;
import java.util.PriorityQueue;
public class PriorityQueueClass {
public static void main(String[] args) {
PriorityQueue a1= new PriorityQueue();
a1.add("Italy");
a1.add("France");
a1.add("Germany");
a1.add("Russia");
a1.add("Australia");
a1.add("England");
System.out.println("Initial List of priorityqueue elements: "+a1);
System.out.println("Removed Element is: "+a1.poll());
System.out.println("New List of priorityqueue elements after using poll method: "+a1);
}
}
Output: Initial List of priorityqueue elements: [Australia, France, England, Russia, Italy, Germany]
Removed Element is: Australia
New List of priorityqueue elements after using poll method: [England, France, Germany, Russia, Italy]
Screenshot:
15. Let us check how peek() method works in PriorityQueue class.
Code Snippet:
package priorityQueueClass;
import java.util.PriorityQueue;
public class PriorityQueueClass {
public static void main(String[] args) {
PriorityQueue a1= new PriorityQueue();
a1.add("Italy");
a1.add("France");
a1.add("Germany");
a1.add("Russia");
a1.add("Australia");
a1.add("England");
System.out.println("Initial List of priorityqueue elements: "+a1);
System.out.println("Element on which peek method is applied: "+a1.peek());
System.out.println("New List of priorityqueue elements after using peek method: "+a1);
}
}
Output: Initial List of priorityqueue elements: [Australia, France, England, Russia, Italy, Germany]
Element on which peek method is applied: Australia
New List of priorityqueue elements after using peek method: [Australia, France, England, Russia, Italy, Germany]
Screenshot:
16. Let us check how element() method works in PriorityQueue class.
Code Snippet:
package priorityQueueClass;
import java.util.PriorityQueue;
public class PriorityQueueClass {
public static void main(String[] args) {
PriorityQueue a1= new PriorityQueue();
a1.add("Italy");
a1.add("France");
a1.add("Germany");
a1.add("Russia");
a1.add("Australia");
a1.add("England");
System.out.println("Initial List of priorityqueue elements: "+a1);
System.out.println("Element on which element method is applied: "+a1.element());
System.out.println("New List of priorityqueue elements after using element method: "+a1);
}
}
Output: Initial List of priorityqueue elements: [Australia, France, England, Russia, Italy, Germany]
Element on which element method is applied: Australia
New List of priorityqueue elements after using element method: [Australia, France, England, Russia, Italy, Germany]
Screenshot:
Conclusion:
PriorityQueue class is used for handling all the Collection elements in a proper manner. It is necessary to understand the usage of all the implemented classes and interfaces of Queue interfaces alongwith the methods for better handling of Collection elements. With in-depth understanding of the PriorityQueue class and its methods result in developers and testers utlilising these concepts in real life tasks. Remember to practise, stay updated with the latest trends in Automation Software Testing Course,and maintain a positive attitude throughout your interview process. Developers could write better program codes for software applications and Testers could write better test scripts for testing the software applications due to usage of PriorityQueue class and its methods.
Also read:
Consult Us