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


Java LinkedTransferQueue removeAll()用法及代碼示例


java.util.concurrent.LinkedTransferQueue的removeAll()方法是Java的內置函數,用於從此隊列中刪除所有包含在指定集合中的元素。

用法:

public boolean removeAll(Collection c)

參數:此方法將集合c作為包含要從此列表中刪除的元素的參數。


返回值:如果此列表由於調用而更改,則此方法返回true。

異常: NullPointerException 如果此列表包含null元素。

以下示例程序旨在說明LinkedTransferQueue類的removeAll()函數:

示例1:

// Java code to illustrate 
// removeAll() method of LinkedTransferQueue 
  
import java.util.concurrent.LinkedTransferQueue; 
import java.util.*; 
  
public class GFG { 
    public static void main(String[] args) 
        throws InterruptedException 
    { 
  
        // create object of LinkedTransferQueue 
        LinkedTransferQueue<String> LTQ 
            = new LinkedTransferQueue<String>(); 
  
        // Add numbers to end of LinkedTransferQueue 
        // using add() method 
        LTQ.add("GeeksforGeeks"); 
        LTQ.add("Geeks"); 
        LTQ.add("Computer Science"); 
        LTQ.add("Portal"); 
        LTQ.add("Gfg"); 
  
        // Print the Queue 
        System.out.println("Linked Transfer Queue : "
                           + LTQ); 
  
        // Get the ArrayList to be deleted 
        ArrayList<String> arraylist 
            = new ArrayList<String>(); 
        arraylist.add("GeeksforGeeks"); 
        arraylist.add("Gfg"); 
        arraylist.add("hack"); 
  
        // Print ArrayList 
        System.out.println("ArrayList to be deleted : "
                           + arraylist); 
  
        // Removing elements from the queue 
        // which are common to arraylist 
        // using removeAll() method. 
        LTQ.removeAll(arraylist); 
  
        // Prints the Queue 
        System.out.println("Linked Transfer Queue "
                           + "after removal of ArrayList : "
                           + LTQ); 
    } 
}
輸出:

Linked Transfer Queue : [GeeksforGeeks, Geeks, Computer Science, Portal, Gfg]
ArrayList to be deleted : [GeeksforGeeks, Gfg, hack]
Linked Transfer Queue after removal of ArrayList : [Geeks, Computer Science, Portal]

示例2:

// Java code to illustrate 
// removeAll() method of LinkedTransferQueue 
  
import java.util.concurrent.LinkedTransferQueue; 
import java.util.*; 
  
public class GFG { 
    public static void main(String[] args) 
        throws InterruptedException 
    { 
  
        // create object of LinkedTransferQueue 
        LinkedTransferQueue<Integer> LTQ 
            = new LinkedTransferQueue<Integer>(); 
  
        // Add numbers to end of LinkedTransferQueue 
        // using add() method 
        LTQ.add(3); 
        LTQ.add(6); 
        LTQ.add(10); 
        LTQ.add(125); 
        LTQ.add(205); 
  
        // print the Queue 
        System.out.println("Linked Transfer Queue : "
                           + LTQ); 
  
        // Get the ArrayList to be deleted 
        ArrayList<Integer> arraylist 
            = new ArrayList<Integer>(); 
        arraylist.add(10); 
        arraylist.add(100); 
        arraylist.add(125); 
  
        // Print ArrayList 
        System.out.println("ArrayList to be deleted : "
                           + arraylist); 
  
        // Removing elements from the queue 
        // which are common to arraylist 
        // using removeAll() method. 
        LTQ.removeAll(arraylist); 
  
        // Prints the Queue 
        System.out.println("Linked Transfer Queue "
                           + "after removal of ArrayList : "
                           + LTQ); 
    } 
}
輸出:

Linked Transfer Queue : [3, 6, 10, 125, 205]
ArrayList to be deleted : [10, 100, 125]
Linked Transfer Queue after removal of ArrayList : [3, 6, 205]

參考: https://docs.oracle.com/javase/9/docs/api/java/util/concurrent/LinkedTransferQueue.html#removeAll-java.util.Collection-



相關用法


注:本文由純淨天空篩選整理自ProgrammerAnvesh大神的英文原創作品 LinkedTransferQueue removeAll() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。