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


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


LinkedBlockingDeque的removeAll()方法是Java的內置函數,用於從此雙端隊列中刪除指定集合中包含的所有元素。這意味著,從此雙端隊列刪除了這兩個集合的所有公共元素。

用法:

public boolean removeAll(Collection c)

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


返回值:如果此雙端隊列由於調用而改變,則此方法返回true。

異常:如果指定的集合為Null,則此方法將引發NULL指針異常。

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

示例1:

// Java Program Demonstrate removeAll() 
// method of LinkedBlockingDeque 
  
import java.util.concurrent.LinkedBlockingDeque; 
import java.util.*; 
  
public class GFG { 
    public static void main(String[] args) 
        throws IllegalStateException 
    { 
  
        // create object of LinkedBlockingDeque 
        LinkedBlockingDeque<Integer> LBD 
            = new LinkedBlockingDeque<Integer>(); 
  
        // Add numbers to end of LinkedBlockingDeque 
        LBD.add(11); 
        LBD.add(22); 
        LBD.add(33); 
        LBD.add(44); 
  
        // print deque 
        System.out.println("Linked Blocking Deque: "
                           + LBD); 
  
        // create object of ArrayList collection 
        ArrayList<Integer> ArrLis 
            = new ArrayList<Integer>(); 
  
        // Add number to ArrayList 
        ArrLis.add(55); 
        ArrLis.add(11); 
        ArrLis.add(23); 
        ArrLis.add(22); 
  
        // print ArrayList 
        System.out.println("ArraList to be removed: "
                           + ArrLis); 
  
        // function removeAll() removes all 
        // the common elements from deque 
        LBD.removeAll(ArrLis); 
  
        // print deque 
        System.out.println("Current Linked Blocking Deque: "
                           + LBD); 
    } 
}
輸出:
Linked Blocking Deque: [11, 22, 33, 44]
ArraList to be removed: [55, 11, 23, 22]
Current Linked Blocking Deque: [33, 44]

示例2:

// Java Program Demonstrate removeAll() 
// method of LinkedBlockingDeque 
  
import java.util.concurrent.LinkedBlockingDeque; 
import java.util.*; 
  
public class GFG { 
    public static void main(String[] args) 
        throws IllegalStateException 
    { 
  
        // create object of LinkedBlockingDeque 
        LinkedBlockingDeque<String> LBD 
            = new LinkedBlockingDeque<String>(); 
  
        // Add numbers to end of LinkedBlockingDeque 
        LBD.add("geeks"); 
        LBD.add("Geeks"); 
        LBD.add("gfg"); 
        LBD.add("Gfg"); 
  
        // print deque 
        System.out.println("Linked Blocking Deque: "
                           + LBD); 
  
        // create object of ArrayList collection 
        ArrayList<String> ArrLis 
            = new ArrayList<String>(); 
  
        // Add number to ArrayList 
        ArrLis.add("GeeksforGeeks"); 
        ArrLis.add("Geeks"); 
        ArrLis.add("gfg"); 
        ArrLis.add("Gfg"); 
  
        // print ArrayList 
        System.out.println("ArraList: "
                           + ArrLis); 
  
        // function removeAll() removes all 
        // the common elements from deque 
        LBD.removeAll(ArrLis); 
  
        // print deque 
        System.out.println("Linked Blocking Deque: "
                           + LBD); 
    } 
}
輸出:
Linked Blocking Deque: [geeks, Geeks, gfg, Gfg]
ArraList: [GeeksforGeeks, Geeks, gfg, Gfg]
Linked Blocking Deque: [geeks]

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



相關用法


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