📌  相关文章
📜  Java中的 LinkedBlockingDeque retainAll() 方法及示例

📅  最后修改于: 2022-05-13 01:54:56.207000             🧑  作者: Mango

Java中的 LinkedBlockingDeque retainAll() 方法及示例

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 LBD
            = new LinkedBlockingDeque();
  
        // 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 ArrLis
            = new ArrayList();
  
        // 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 LBD
            = new LinkedBlockingDeque();
  
        // 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 ArrLis
            = new ArrayList();
  
        // 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]

参考: https://docs.oracle.com/javase/9/docs/api/ Java/util/concurrent/LinkedBlockingDeque.html#retainAll-java.util.Collection-