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


Java LinkedBlockingDeque addAll()用法及代碼示例


LinkedBlockingDeque的addAll()方法將指定集合的​​所有元素附加到此雙端隊列的末尾。

用法:

public void addAll(Collection<E> c)

參數:此方法接受強製參數c,該參數是要在LinkedBlockingDeque末尾插入的集合。


返回值:此方法不返回任何內容。

異常:存在2個異常:

  • IllegalStateException:如果由於容量限製此時無法添加集合。
  • NullPointerException :如果指定的集合為Null。

以下示例程序旨在說明LinkedBlockingDeque類的addAll()函數:

範例1:

// Java Program Demonstrate addAll() 
// 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(66); 
        ArrLis.add(77); 
        ArrLis.add(88); 
  
        // Print ArrayList 
        System.out.println("ArraList: "
                           + ArrLis); 
  
        // Function addAll() adds all the elements of 
        // ArrayList to Deque 
        LBD.addAll(ArrLis); 
  
        // Print deque 
        System.out.println("Linked Blocking Deque: "
                           + LBD); 
    } 
}

範例2:

// Java Program Demonstrate addAll() 
// 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 elements to end of LinkedBlockingDeque 
        LBD.add("GeeksforGeeks"); 
        LBD.add("Gfg"); 
        LBD.add("Geeks"); 
  
        // 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("Computer"); 
        ArrLis.add("Science"); 
        ArrLis.add("Portal"); 
  
        // Print ArrayList 
        System.out.println("ArraList: " + ArrLis); 
  
        // Function addAll() adds all the elements of 
        // ArrayList to Deque 
        LBD.addAll(ArrLis); 
  
        // Print deque 
        System.out.println("Linked Blocking Deque: "
                           + LBD); 
    } 
}

參考: https://docs.oracle.com/javase/9/docs/api/java/util/concurrent/LinkedBlockingDeque.html#addAll-java.util.Collection-



相關用法


注:本文由純淨天空篩選整理自ProgrammerAnvesh大神的英文原創作品 LinkedBlockingDeque addAll() method in Java with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。