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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。