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


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


ConcurrentLinkedQueue的addAll()方法用於在ConcurrentLinkedQueue的末尾插入作為參數傳遞給此方法的Collection的所有元素。元素的插入順序與集合迭代器返回的順序相同。

用法:

public boolean addAll(Collection<? extends E> c)

參數:此方法采用參數c,該參數表示需要在此ConcurrentLinkedQueue的末尾附加其元素的集合。


返回值:如果執行了至少一個插入操作,則此方法返回true。

異常:此方法引發兩個不同的異常:

  • NullPointerException –如果傳遞的集合或其任何元素為null。
  • IllegalArgumentException–如果傳遞的集合是此隊列本身。

以下示例程序旨在說明ConcurrentLinkedQueue的addAll()方法:

示例1:嘗試將數字列表添加到ConcurrentLinkedQueue。

// Java Program Demonstrate addAll() 
// method of ConcurrentLinkedQueue 
  
import java.util.concurrent.*; 
import java.util.*; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // create an ConcurrentLinkedQueue 
        ConcurrentLinkedQueue<Integer> 
            queue = new ConcurrentLinkedQueue<Integer>(); 
  
        // Add Numbers to queue 
        queue.add(4353); 
  
        // create a ArrayList of Numbers 
        ArrayList<Integer> arraylist = new ArrayList<Integer>(); 
  
        // add some numbers to ArrayList 
        arraylist.add(377139); 
        arraylist.add(624378); 
        arraylist.add(654793); 
        arraylist.add(764764); 
        arraylist.add(838494); 
        arraylist.add(632845); 
  
        // Displaying the existing ConcurrentLinkedQueue 
        System.out.println("ConcurrentLinkedQueue: " + queue); 
  
        // Displaying the existing Collection 
        System.out.println("Collection to be added: " + arraylist); 
  
        // apply addAll() method and passed the arraylist as parameter 
        boolean response = queue.addAll(arraylist); 
  
        // Displaying the existing ConcurrentLinkedQueue 
        System.out.println("Collection added: " + response); 
  
        // Displaying the existing ConcurrentLinkedQueue 
        System.out.println("ConcurrentLinkedQueue: " + queue); 
    } 
}
輸出:
ConcurrentLinkedQueue: [4353]
Collection to be added: [377139, 624378, 654793, 764764, 838494, 632845]
Collection added: true
ConcurrentLinkedQueue: [4353, 377139, 624378, 654793, 764764, 838494, 632845]

示例2:嘗試將字符串列表添加到ConcurrentLinkedQueue。

// Java Program Demonstrate addAll() 
// method of ConcurrentLinkedQueue 
  
import java.util.concurrent.*; 
import java.util.*; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // create an ConcurrentLinkedQueue 
        ConcurrentLinkedQueue<String> 
            queue = new ConcurrentLinkedQueue<String>(); 
  
        // Add String to queue 
        queue.add("Aman"); 
        queue.add("Amar"); 
  
        // create a ArrayList of Numbers 
        ArrayList<String> arraylist = new ArrayList<String>(); 
  
        // add some numbers to ArrayList 
  
        arraylist.add("Sanjeet"); 
        arraylist.add("Rabi"); 
        arraylist.add("Debasis"); 
        arraylist.add("Raunak"); 
        arraylist.add("Mahesh"); 
  
        // Displaying the existing ConcurrentLinkedQueue 
        System.out.println("ConcurrentLinkedQueue: " + queue); 
  
        // Displaying the existing Collection 
        System.out.println("Collection to be added: " + arraylist); 
  
        // apply addAll() method and passed the arraylist as parameter 
        boolean response = queue.addAll(arraylist); 
  
        // Displaying the existing ConcurrentLinkedQueue 
        System.out.println("Collection added: " + response); 
  
        // Displaying the existing ConcurrentLinkedQueue 
        System.out.println("ConcurrentLinkedQueue: " + queue); 
    } 
}
輸出:
ConcurrentLinkedQueue: [Aman, Amar]
Collection to be added: [Sanjeet, Rabi, Debasis, Raunak, Mahesh]
Collection added: true
ConcurrentLinkedQueue: [Aman, Amar, Sanjeet, Rabi, Debasis, Raunak, Mahesh]

示例3:顯示NullPointerException

// Java Program Demonstrate addAll() 
// method of ConcurrentLinkedQueue 
  
import java.util.concurrent.*; 
import java.util.*; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // create an ConcurrentLinkedQueue 
        ConcurrentLinkedQueue<String> 
            queue = new ConcurrentLinkedQueue<String>(); 
  
        // Add String to queue 
        queue.add("Aman"); 
        queue.add("Amar"); 
  
        // create a ArrayList which is equal to null 
        ArrayList<String> arraylist = null; 
  
        // Displaying the existing ConcurrentLinkedQueue 
        System.out.println("ConcurrentLinkedQueue: " + queue); 
  
        // Displaying the existing Collection 
        System.out.println("Collection to be added: " + arraylist); 
  
        try { 
            // apply addAll() method and pass the arraylist as parameter 
            // since the arraylist is null, exception will be thrown 
            boolean response = queue.addAll(arraylist); 
        } 
        catch (NullPointerException e) { 
            System.out.println("Exception thrown while adding null: " + e); 
        } 
    } 
}
輸出:
ConcurrentLinkedQueue: [Aman, Amar]
Collection to be added: null
Exception thrown while adding null: java.lang.NullPointerException

示例4:顯示IllegalArgumentException

// Java Program Demonstrate addAll() 
// method of ConcurrentLinkedQueue 
  
import java.util.concurrent.*; 
import java.util.*; 
  
public class GFG { 
    public static void main(String[] args) 
    { 
  
        // create an ConcurrentLinkedQueue 
        ConcurrentLinkedQueue<String> 
            queue = new ConcurrentLinkedQueue<String>(); 
  
        // Add String to queue 
        queue.add("Aman"); 
        queue.add("Amar"); 
  
        // Displaying the existing ConcurrentLinkedQueue 
        System.out.println("ConcurrentLinkedQueue: " + queue); 
  
        try { 
            // apply addAll() method and passed the queue as parameter 
            boolean response = queue.addAll(queue); 
        } 
        catch (IllegalArgumentException e) { 
            System.out.println("Exception thrown while adding queue"
                               + " to itself when collection is required: " + e); 
        } 
    } 
}
輸出:
ConcurrentLinkedQueue: [Aman, Amar]
Exception thrown 
 while adding queue to itself
 when collection is required: java.lang.IllegalArgumentException

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



相關用法


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