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


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


ConcurrentLinkedDeque的addAll(Collection col),它以col作為參數,其中col是元素(列表,數組列表,鏈接列表等)的集合。整個集合會被追加或添加到出隊的末尾。如果將Collection附加到雙端隊列,則此方法就像add()方法將返回true。

用法:

public boolean addAll(Collection c)

參數:該函數接受參數col,該參數是要添加的元素的集合。


返回值:此方法返回一個布爾值,說明是否將Collection附加到Deque。

異常:此方法接受兩個異常:

  • NullPointerException :如果集合中存在的任何元素為null或集合為null。
  • IllegalArgumentException:如果傳遞的集合本身是現有雙端隊列。

以下示例說明了addAll()方法:

範例1:將數字集合添加到ConcurrentLinkedDeque的第一個集合中

// Java program to illustrate addAll() method 
  
import java.util.concurrent.ConcurrentLinkedDeque; 
  
public class Example1 { 
    public static void main(String[] args) 
    { 
  
        // create object of ConcurrentLinkedDeque 
        ConcurrentLinkedDeque<Integer> ld1 
            = new ConcurrentLinkedDeque<Integer>(); 
  
        // Add elements 
        ld1.add(1); 
        ld1.add(2); 
        ld1.add(3); 
        ld1.add(4); 
  
        // print ld1 
        System.out.println("LD1: " + ld1); 
  
        // create object of ConcurrentLinkedDeque 
        ConcurrentLinkedDeque<Integer> ld2 
            = new ConcurrentLinkedDeque<Integer>(); 
  
        ld2.add(8); 
        ld2.add(1); 
        ld2.add(3); 
        ld2.add(5); 
  
        // print ld2 
        System.out.println("LD2: " + ld2); 
  
        // Adding elements of ld2 to ld1 at its end. 
        ld1.addAll(ld2); 
  
        System.out.println("After adding the ld2 to ld1: "
                           + ld1); 
    } 
}
輸出:
LD1: [1, 2, 3, 4]
LD2: [8, 1, 3, 5]
After adding the ld2 to ld1: [1, 2, 3, 4, 8, 1, 3, 5]

範例2:顯示IllegalArgumentException

// Java program to illustrate addAll() method 
  
import java.util.concurrent.ConcurrentLinkedDeque; 
  
public class Example2 { 
    public static void main(String[] args) 
    { 
        // create object of ConcurrentLinkedDeque 
        ConcurrentLinkedDeque<String> ld1 
            = new ConcurrentLinkedDeque<String>(); 
  
        // Add elements 
        ld1.add("John"); 
        ld1.add("Johnathan"); 
        ld1.add("Jones"); 
        ld1.add("James"); 
  
        // print ld1 
        System.out.println("LD1: " + ld1); 
  
        try { 
            ld1.addAll(ld1); 
        } 
        catch (IllegalArgumentException e) { 
            System.out.println("Exception thrown while "
                               + "adding deque to "
                               + "itself is: "
                               + e); 
        } 
    } 
}
輸出:
LD1: [John, Johnathan, Jones, James]
Exception thrown while adding deque to itself is: java.lang.IllegalArgumentException

範例3:顯示NullPointerException

// Java program to illustrate addAll() method 
  
import java.util.concurrent.ConcurrentLinkedDeque; 
  
public class Example3 { 
    public static void main(String[] args) 
    { 
  
        // create object of ConcurrentLinkedDeque 
        ConcurrentLinkedDeque<String> ld1 
            = new ConcurrentLinkedDeque<String>(); 
  
        // Add elements 
        ld1.add("John"); 
        ld1.add("Johnathan"); 
        ld1.add("Jones"); 
        ld1.add("James"); 
  
        // print ld1 
        System.out.println("LD1: " + ld1); 
  
        // create object of ConcurrentLinkedDeque 
        ConcurrentLinkedDeque<String> ld2 = null; 
  
        // print ld2 
        System.out.println("LD2: " + ld2); 
  
        try { 
  
            System.out.println("After adding the ld2 to ld1: "); 
            ld2.addAll(ld1); 
        } 
        catch (NullPointerException e) { 
            System.out.println(e); 
        } 
    } 
}
輸出:
LD1: [John, Johnathan, Jones, James]
LD2: null
After adding the ld2 to ld1: 
java.lang.NullPointerException

參考: https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/ConcurrentLinkedDeque.html



相關用法


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