
- Java.util - Home
- Java.util - ArrayDeque
- Java.util - ArrayList
- Java.util - Arrays
- Java.util - BitSet
- Java.util - Calendar
- Java.util - Collections
- Java.util - Currency
- Java.util - Date
- Java.util - Dictionary
- Java.util - EnumMap
- Java.util - EnumSet
- Java.util - Formatter
- Java.util - GregorianCalendar
- Java.util - HashMap
- Java.util - HashSet
- Java.util - Hashtable
- Java.util - IdentityHashMap
- Java.util - LinkedHashMap
- Java.util - LinkedHashSet
- Java.util - LinkedList
- Java.util - ListResourceBundle
- Java.util - Locale
- Java.util - Observable
- Java.util - PriorityQueue
- Java.util - Properties
- Java.util - PropertyPermission
- Java.util - PropertyResourceBundle
- Java.util - Random
- Java.util - ResourceBundle
- Java.util - ResourceBundle.Control
- Java.util - Scanner
- Java.util - ServiceLoader
- Java.util - SimpleTimeZone
- Java.util - Stack
- Java.util - StringTokenizer
- Java.util - Timer
- Java.util - TimerTask
- Java.util - TimeZone
- Java.util - TreeMap
- Java.util - TreeSet
- Java.util - UUID
- Java.util - Vector
- Java.util - WeakHashMap
- Java.util - Interfaces
- Java.util - Exceptions
- Java.util - Enumerations
- Java.util Useful Resources
- Java.util - Useful Resources
- Java.util - Discussion
Java PriorityQueue clear() Method
Description
The Java PriorityQueue clear() method removes all of the elements from this queue. This method is very useful when we want to reuse a queue or reset the queue.
Declaration
Following is the declaration for java.util.PriorityQueue.clear() method
public void clear()
Parameters
NA
Return Value
This method does not return any value.
Exception
NA
Clearing a PriorityQueue of Ints Example
The following example shows the usage of Java PriorityQueue clear() method. In this example, we're using Integers. As first step, we're populating the arraydeque object and printing it. Then we're print the size of the arraydeque object and perform clear operation. After clearing, we're printing the size of the arraydeque object which is 0 now.
package com.tutorialspoint; import java.util.PriorityQueue; public class PriorityQueueDemo { public static void main(String[] args) { // create an empty priority queue PriorityQueue<Integer> queue = new PriorityQueue<>(); // use add() method to add elements in the queue queue.add(20); queue.add(30); queue.add(10); // let us print all the elements available in queue System.out.println("PriorityQueue = " + queue); // finding size of this queue int retval = queue.size(); System.out.println("PriorityQueue consists of "+ retval +" elements"); System.out.println("Performing clear operation !!"); queue.clear(); retval = queue.size(); System.out.println("Now, queue consists of "+ retval +" elements"); } }
Output
Let us compile and run the above program, this will produce the following result −
PriorityQueue = [10, 30, 20] PriorityQueue consists of 3 elements Performing clear operation !! Now, queue consists of 0 elements
Clearing a PriorityQueue of Strings Example
The following example shows the usage of Java PriorityQueue clear() method. In this example, we're using Strings. As first step, we're populating the arraydeque object and printing it. Then we're print the size of the arraydeque object and perform clear operation. After clearing, we're printing the size of the arraydeque object which is 0 now.
package com.tutorialspoint; import java.util.PriorityQueue; public class PriorityQueueDemo { public static void main(String[] args) { // create an empty priority queue PriorityQueue<String> queue = new PriorityQueue<>(); // use add() method to add elements in the queue queue.add("A"); queue.add("B"); queue.add("C"); // let us print all the elements available in queue System.out.println("PriorityQueue = " + queue); // finding size of this queue int retval = queue.size(); System.out.println("PriorityQueue consists of "+ retval +" elements"); System.out.println("Performing clear operation !!"); queue.clear(); retval = queue.size(); System.out.println("Now, queue consists of "+ retval +" elements"); } }
Output
Let us compile and run the above program, this will produce the following result −
PriorityQueue = [A, B, C] PriorityQueue consists of 3 elements Performing clear operation !! Now, queue consists of 0 elements
Clearing a PriorityQueue of Objects Example
The following example shows the usage of Java PriorityQueue clear() method. In this example, we're using Student objects. As first step, we're populating the arraydeque object and printing it. Then we're print the size of the arraydeque object and perform clear operation. After clearing, we're printing the size of the arraydeque object which is 0 now.
package com.tutorialspoint; import java.util.PriorityQueue; public class PriorityQueueDemo { public static void main(String[] args) { // create an empty priority queue PriorityQueue<Student> queue = new PriorityQueue<>(); // use add() method to add elements in the queue queue.add(new Student(1, "Julie")); queue.add(new Student(2, "Robert")); queue.add(new Student(3, "Adam")); // let us print all the elements available in queue System.out.println("PriorityQueue = " + queue); // finding size of this queue int retval = queue.size(); System.out.println("PriorityQueue consists of "+ retval +" elements"); System.out.println("Performing clear operation !!"); queue.clear(); retval = queue.size(); System.out.println("Now, queue consists of "+ retval +" elements"); } } class Student implements Comparable<Student> { int rollNo; String name; Student(int rollNo, String name){ this.rollNo = rollNo; this.name = name; } @Override public String toString() { return "[ " + this.rollNo + ", " + this.name + " ]"; } @Override public boolean equals(Object obj) { Student s = (Student)obj; return this.rollNo == s.rollNo && this.name.equalsIgnoreCase(s.name); } @Override public int compareTo(Student student) { return this.rollNo - student.rollNo; } }
Output
Let us compile and run the above program, this will produce the following result −
PriorityQueue = [[ 1, Julie ], [ 2, Robert ], [ 3, Adam ]] PriorityQueue consists of 3 elements Performing clear operation !! Now, queue consists of 0 elements