GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.tutorialspoint.com — Java PriorityQueue remove() Method
Selected Reading
Java PriorityQueue remove() MethodDescriptionThe java PriorityQueue remove() method retrieves and removes the head of the queue represented by this queue. DeclarationFollowing is the declaration for java.util.PriorityQueue.remove() method public E remove() ParametersNA Return ValueThis method returns the head of the queue represented by this queue. ExceptionNoSuchElementException − if this queue is empty. remove() method with Object as parameterDescriptionThe Java PriorityQueue remove(Object) method removes a single instance of the specified element from this queue. DeclarationFollowing is the declaration for java.util.PriorityQueue.remove(o) method public boolean remove(Object o) Parameterso − The element to be removed from this queue, if present Return ValueThis method returns true if this queue contains the specified element. ExceptionNA Removing First Element from the PriorityQueue of Ints ExampleThe 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);
}
}
OutputLet 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 ExampleThe 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);
}
}
OutputLet 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 ExampleThe 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;
}
}
OutputLet 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 ]] java_util_priorityqueue.htm Advertisements
|
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |