Java中的 ConcurrentLinkedQueue remove() 方法
ConcurrentLinkedQueue的remove(Object o)方法用于从该 ConcurrentLinkedQueue 中移除指定元素的单个实例(如果存在)。如果此 ConcurrentLinkedQueue 包含一个或多个这样的元素,则此方法删除元素 e 使得 o.equals(e)。如果此 ConcurrentLinkedQueue 包含指定的元素,Remove() 方法将返回 true,否则将返回 false。
句法:
public boolean remove(Object o)
参数:此方法采用参数o ,该参数表示要从此 ConcurrentLinkedQueue 中删除的元素。
返回:如果此 ConcurrentLinkedQueue 包含指定元素并被删除,则此方法返回true 。
下面的程序说明了 ConcurrentLinkedQueue 的 remove() 方法:
示例 1:
// Java Program Demonstrate remove()
// method of ConcurrentLinkedQueue
import java.util.concurrent.*;
public class GFG {
public static void main(String[] args)
{
// create an ConcurrentLinkedQueue
ConcurrentLinkedQueue
queue = new ConcurrentLinkedQueue();
// Add Numbers to queue
queue.add(4353);
queue.add(7824);
queue.add(78249);
queue.add(8724);
// Displaying the existing ConcurrentLinkedQueue
System.out.println("ConcurrentLinkedQueue: " + queue);
// apply remove() for Number 78249
boolean response = queue.remove(78249);
// print results
System.out.println("Removing Number 78249 successful: " + response);
// Displaying the existing ConcurrentLinkedQueue
System.out.println("Updated ConcurrentLinkedQueue: " + queue);
}
}
输出:
ConcurrentLinkedQueue: [4353, 7824, 78249, 8724]
Removing Number 78249 successful: true
Updated ConcurrentLinkedQueue: [4353, 7824, 8724]
示例 2:
// Java Program Demonstrate remove()
// method of ConcurrentLinkedQueue
import java.util.concurrent.*;
public class GFG {
public static void main(String[] args)
{
// create an ConcurrentLinkedQueue
ConcurrentLinkedQueue
queue = new ConcurrentLinkedQueue();
// Add String to queue
queue.add("Aman");
queue.add("Amar");
queue.add("Sanjeet");
queue.add("Rabi");
// Displaying the existing ConcurrentLinkedQueue
System.out.println("ConcurrentLinkedQueue: " + queue);
// apply remove() on queue
boolean response1 = queue.remove("Aman");
// print after applying poll method
System.out.println("Removing String \"Aman\" successful: " + response1);
// Displaying the existing ConcurrentLinkedQueue
System.out.println("Updated ConcurrentLinkedQueue: " + queue);
// apply remove() on queue
boolean response2 = queue.remove("Kisan");
// print after applying poll method
System.out.println("Removing String \"Kisan\" successful: " + response2);
// Displaying the existing ConcurrentLinkedQueue
System.out.println("Updated ConcurrentLinkedQueue: " + queue);
}
}
输出:
ConcurrentLinkedQueue: [Aman, Amar, Sanjeet, Rabi]
Removing String "Aman" successful: true
Updated ConcurrentLinkedQueue: [Amar, Sanjeet, Rabi]
Removing String "Kisan" successful: false
Updated ConcurrentLinkedQueue: [Amar, Sanjeet, Rabi]
参考: https: Java/util/concurrent/ConcurrentLinkedQueue.html#remove-java.lang.Object-