当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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<Integer> 
            queue = new ConcurrentLinkedQueue<Integer>(); 
  
        // 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<String> 
            queue = new ConcurrentLinkedQueue<String>(); 
  
        // 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://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentLinkedQueue.html#remove-java.lang.Object-



相关用法


注:本文由纯净天空筛选整理自AmanSingh2210大神的英文原创作品 ConcurrentLinkedQueue remove() method in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。