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


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。