PriorityQueue类remove()方法 (PriorityQueue Class remove() method)
remove() method is available in java.util package.
remove()方法在java.util包中可用。
remove() method is used to remove one instance of the given object from this PriorityQueue when exists.
remove()方法用于从该PriorityQueue中删除给定对象的一个实例(如果存在)。
remove() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
remove()方法是一种非静态方法,只能通过类对象访问,如果尝试使用类名称访问该方法,则会收到错误消息。
remove() method does not throw an exception at the time of removing an object.
remove()方法在删除对象时不会引发异常。
Syntax:
句法:
public boolean remove(Object ob);
Parameter(s):
参数:
Object ob – represents the element (ele) to be remove from this PriorityQueue.
对象ob –表示要从此PriorityQueue中删除的元素(ele)。
Return value:
返回值:
The return type of the method is boolean, it returns true when the given element is to be removed successfully otherwise it returns false.
方法的返回类型为boolean ,如果要成功删除给定元素,则返回true,否则返回false。
Example:
例:
// Java program to demonstrate the example
// of boolean remove(Object ob) method of
// PriorityQueue
import java.util.*;
public class RemoveOfPriorityQueue {
public static void main(String args[]) {
// Instantiate PriorityQueue
PriorityQueue < String > pq = new PriorityQueue < String > ();
// By using add() method is add
// the given element into priority
// queue
pq.add("C");
pq.add("C++");
pq.add("JAVA");
pq.add("PHP");
pq.add("ANDROID");
pq.add("JAVA");
// Display PriorityQueue
System.out.println("PriorityQueue: " + pq);
// By using remove(object) method is to
// remove the single instance of given element
// from this PriorityQueue
Object ele = pq.remove("JAVA");
// Display removed element
System.out.println("pq.remove(JAVA): " + ele);
// Display PriorityQueue
System.out.println("PriorityQueue: " + pq);
}
}
Output
输出量
PriorityQueue: [ANDROID, C, JAVA, PHP, C++, JAVA]
pq.remove(JAVA): true
PriorityQueue: [ANDROID, C, JAVA, PHP, C++]
翻译自: https://www.includehelp.com/java/priorityqueue-remove-method-with-example.aspx