當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。