LinkedBlockingDeque的retainAll()方法是Java的內置函數,用於從此雙端隊列中刪除指定集合中包含的所有元素。這意味著,從此雙端隊列刪除了這兩個集合的所有公共元素。
用法:
public boolean retainAll(Collection c)
參數:此方法將集合c作為包含要從此列表中刪除的元素的參數。
返回值:如果此雙端隊列由於調用而改變,則此方法返回true。
異常:如果指定的集合為Null,則此方法將引發NULL指針異常。
以下示例程序旨在說明LinkedBlockingDeque類的retainAll()函數:
示例1:
// Java Program Demonstrate retainAll()
// 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("ArrayList to be retained: "
+ ArrLis);
// Function retainAll() retains
// all the common elements from deque
LBD.retainAll(ArrLis);
// Print deque
System.out.println("Retained Linked Blocking Deque: "
+ LBD);
}
}
輸出:
Linked Blocking Deque: [11, 22, 33, 44] ArrayList to be retained: [55, 11, 23, 22] Retained Linked Blocking Deque: [11, 22]
示例2:
// Java Program Demonstrate retainAll()
// 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 elements to ArrayList
ArrLis.add("GeeksforGeeks");
ArrLis.add("Geeks");
ArrLis.add("gfg");
ArrLis.add("Gfg");
// Print ArrayList
System.out.println("ArrayList to be retained: "
+ ArrLis);
// Function retainAll() retains
// all the common elements from deque
LBD.retainAll(ArrLis);
// Print deque
System.out.println("Retained Linked Blocking Deque: "
+ LBD);
}
}
輸出:
Linked Blocking Deque: [geeks, Geeks, gfg, Gfg] ArrayList to be retained: [GeeksforGeeks, Geeks, gfg, Gfg] Retained Linked Blocking Deque: [Geeks, gfg, Gfg]
相關用法
- Java LinkedTransferQueue retainAll()用法及代碼示例
- Java SortedSet retainAll()用法及代碼示例
- Java Vector retainAll()用法及代碼示例
- Java CopyOnWriteArrayList retainAll()用法及代碼示例
- Java List retainAll()用法及代碼示例
- Java AbstractCollection retainAll()用法及代碼示例
- Java LinkedBlockingDeque removeAll()用法及代碼示例
- Java LinkedBlockingDeque forEach()用法及代碼示例
- Java LinkedBlockingDeque removeIf()用法及代碼示例
- Java LinkedBlockingDeque addAll()用法及代碼示例
- Java Set retainAll()用法及代碼示例
- Java TreeSet retainAll()用法及代碼示例
- Java AbstractSequentialList retainAll()用法及代碼示例
- Java HashSet retainAll()用法及代碼示例
- Java AbstractSet retainAll()用法及代碼示例
注:本文由純淨天空篩選整理自ProgrammerAnvesh大神的英文原創作品 LinkedBlockingDeque retainAll() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。