当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java LinkedTransferQueue retainAll()用法及代码示例


java.util.concurrent.LinkedTransferQueue的retainAll()方法是Java中的内置函数,用于保留此队列中所有对指定集合和此队列均通用的元素。所有其他不常见的元素都将从此队列中删除。

用法:

public boolean retainAll(Collection C)

参数:此方法采用参数C,该参数C是包含要保留在队列中的元素的集合。


返回值:如果由于调用而导致队列完全更改,则该方法返回布尔值true,否则返回false。

异常:此方法引发以下异常:

  • ClassCastException:如果此Queue元素的类与Passed集合不兼容。
  • NullPointerException :如果指定的集合为Null。

以下示例程序旨在说明LinkedTransferQueue类的retainAll()函数:

示例1:

// Java code to illustrate 
// retainAll() 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 retained 
        ArrayList<Integer> arraylist 
            = new ArrayList<Integer>(); 
        arraylist.add(10); 
        arraylist.add(100); 
        arraylist.add(125); 
  
        // Print ArrayList 
        System.out.println("ArrayList to be retained : "
                           + arraylist); 
  
        // Retaining elements from the queue 
        // which are common to arraylist 
        // using retainAll() method. 
        LTQ.retainAll(arraylist); 
  
        // Prints the Queue 
        System.out.println("Linked Transfer Queue "
                           + "after retaining ArrayList : "
                           + LTQ); 
    } 
}
输出:
Linked Transfer Queue : [3, 6, 10, 125, 205]
ArrayList to be retained : [10, 100, 125]
Linked Transfer Queue after retaining ArrayList : [10, 125]

示例2:

// Java code to illustrate 
// retainAll() 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("Geek"); 
        LTQ.add("Gfg"); 
        LTQ.add("Computer"); 
        LTQ.add("Science"); 
  
        // Print the Queue 
        System.out.println("Linked Transfer Queue : "
                           + LTQ); 
  
        // Get the ArrayList to be retained 
        ArrayList<String> arraylist 
            = new ArrayList<String>(); 
        arraylist.add("Gfg"); 
        arraylist.add("Science"); 
        arraylist.add("Computer"); 
  
        // Print ArrayList 
        System.out.println("ArrayList to be retained : "
                           + arraylist); 
  
        // Retaining elements from the queue 
        // which are common to arraylist 
        // using retainAll() method. 
        LTQ.retainAll(arraylist); 
  
        // Prints the Queue 
        System.out.println("Linked Transfer Queue "
                           + "after retaining ArrayList : "
                           + LTQ); 
    } 
}
输出:
Linked Transfer Queue : [GeeksforGeeks, Geek, Gfg, Computer, Science]
ArrayList to be retained : [Gfg, Science, Computer]
Linked Transfer Queue after retaining ArrayList : [Gfg, Computer, Science]

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



相关用法


注:本文由纯净天空筛选整理自ProgrammerAnvesh大神的英文原创作品 LinkedTransferQueue retainAll() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。