
- 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 remove() Method
Description
The java PriorityQueue remove() method retrieves and removes the head of the queue represented by this queue.
Declaration
Following is the declaration for java.util.PriorityQueue.remove() method
public E remove()
Parameters
NA
Return Value
This method returns the head of the queue represented by this queue.
Exception
NoSuchElementException − if this queue is empty.
remove() method with Object as parameter
Description
The Java PriorityQueue remove(Object) method removes a single instance of the specified element from this queue.
Declaration
Following is the declaration for java.util.PriorityQueue.remove(o) method
public boolean remove(Object o)
Parameters
o − The element to be removed from this queue, if present
Return Value
This method returns true if this queue contains the specified element.
Exception
NA
Removing First Element from the PriorityQueue of Ints Example
The following example shows the usage of Java PriorityQueue remove() method with Integers. We're creating an PriorityQueue of Integers, adding some elements, print it and then use remove() method to remove the first element. As PriorityQueue is modified it is printed to check if first element is removed or not.
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(25); queue.add(30); queue.add(20); queue.add(18); // let us print all the elements available in queue System.out.println("PriorityQueue = " + queue); // it will retrieve first element after removing from queue System.out.println("Retrieved Element is = " + queue.remove()); // let us print all the elements available in queue again System.out.println("PriorityQueue = " + queue); } }
Output
Let us compile and run the above program, this will produce the following result −
PriorityQueue = [18, 20, 25, 30] Retrieved Element is = 18 PriorityQueue = [20, 30, 25]
Removing First Element from the PriorityQueue of Strings Example
The following example shows the usage of Java PriorityQueue remove() method with Strings. We're creating an PriorityQueue of String, adding some elements, print it and then use remove() method to get the first element. As PriorityQueue is modified it is printed to check if first element is removed or not.
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"); queue.add("D"); // let us print all the elements available in queue System.out.println("PriorityQueue = " + queue); // it will retrieve first element after removing from queue System.out.println("Retrieved Element is = " + queue.remove()); // let us print all the elements available in queue again System.out.println("PriorityQueue = " + queue); } }
Output
Let us compile and run the above program, this will produce the following result −
PriorityQueue = [A, B, C, D] Retrieved Element is = A PriorityQueue = [B, D, C]
Removing First Element from the PriorityQueue of Objects Example
The following example shows the usage of Java PriorityQueue remove(object) method with Student objects. We're creating an PriorityQueue of Student, adding some elements, print it and then use remove(object) method to get the a particular student removed. As PriorityQueue is modified it is printed to check if that student object is removed or not.
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); // it will return true after removing Robert from queue System.out.println("Student removed :" + queue.remove(new Student(2, "Robert"))); // let us print all the elements available in queue again System.out.println("PriorityQueue = " + queue); } } 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 ]] Student removed : true PriorityQueue = [[ 1, Julie ], [ 3, Adam ]]