Java中的ConcurrentLinkedQueue addAll() 方法
ConcurrentLinkedQueue的 addAll() 方法用于在ConcurrentLinkedQueue的末尾插入作为参数传递的集合的所有元素。元素的插入顺序与集合迭代器返回的顺序相同。
语法:
public boolean addAll(Collection<? extends E> c)
参数: 此方法需要一个参数 c ,它代表需要附加到此ConcurrentLinkedQueue末尾的集合。
返回值: 如果插入至少一项操作,则此方法返回true。
异常: 此方法抛出两种不同的异常:
- NullPointerException – 如果传递的集合或其任何元素为null。
- IllegalArgumentException – 如果传递的集合为此队列本身。
下面的程序说明了ConcurrentLinkedQueue的addAll()方法:
示例 1: 尝试向ConcurrentLinkedQueue添加一些数字列表。
// Java程序演示ConcurrentLinkedQueue的addAll()方法
import java.util.concurrent.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// 创建一个ConcurrentLinkedQueue
ConcurrentLinkedQueue<Integer>
queue = new ConcurrentLinkedQueue<Integer>();
// 添加数字到队列中
queue.add(4353);
// 创建一个数字Arraylist
ArrayList<Integer> arraylist = new ArrayList<Integer>();
// 向ArrayList中添加一些数字
arraylist.add(377139);
arraylist.add(624378);
arraylist.add(654793);
arraylist.add(764764);
arraylist.add(838494);
arraylist.add(632845);
// 显示现有的ConcurrentLinkedQueue
System.out.println("ConcurrentLinkedQueue: " + queue);
// 显示现有的Collection
System.out.println("Collection to be added: " + arraylist);
// 应用addAll()方法并将arraylist作为参数传递
boolean response = queue.addAll(arraylist);
// 显示现有的ConcurrentLinkedQueue
System.out.println("Collection added: " + response);
// 显示现有的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程序演示ConcurrentLinkedQueue的addAll()方法
import java.util.concurrent.*;
import java.util.*;
public class GFG {
public static void main(String[] args) {
//创建ConcurrentLinkedQueue
ConcurrentLinkedQueue<String>
queue = new ConcurrentLinkedQueue<String>();
//向队列中添加字符串
queue.add("Aman");
queue.add("Amar");
//创建一个数字的ArrayList
ArrayList<String> arraylist = new ArrayList<String>();
//向ArrayList中添加数字
arraylist.add("Sanjeet");
arraylist.add("Rabi");
arraylist.add("Debasis");
arraylist.add("Raunak");
arraylist.add("Mahesh");
//显示现有的ConcurrentLinkedQueue
System.out.println("ConcurrentLinkedQueue: " + queue);
//显示现有的Collection
System.out.println("要添加的Collection: " + arraylist);
//应用addAll()方法,并将arraylist作为参数传递
boolean response = queue.addAll(arraylist);
//显示现有的ConcurrentLinkedQueue
System.out.println("添加的Collection: " + response);
//显示现有的ConcurrentLinkedQueue
System.out.println("ConcurrentLinkedQueue: " + queue);
}
}
ConcurrentLinkedQueue: [Aman, Amar]
要添加的Collection: [Sanjeet, Rabi, Debasis, Raunak, Mahesh]
添加的Collection: true
ConcurrentLinkedQueue: [Aman, Amar, Sanjeet, Rabi, Debasis, Raunak, Mahesh]
示例3: 显示空指针异常
//Java程序演示ConcurrentLinkedQueue的addAll()方法
import java.util.concurrent.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
//创建ConcurrentLinkedQueue
ConcurrentLinkedQueue<String>
queue = new ConcurrentLinkedQueue<String>();
//向队列中添加字符串
queue.add("Aman");
queue.add("Amar");
//创建一个等于null的ArrayList
ArrayList<String> arraylist = null;
//显示现有的ConcurrentLinkedQueue
System.out.println("ConcurrentLinkedQueue: " + queue);
//显示现有的Collection
System.out.println("要添加的Collection: " + arraylist);
try {
//应用addAll()方法并将arraylist作为参数传递
//由于arraylist为空,会抛出异常
boolean response = queue.addAll(arraylist);
}
catch (NullPointerException e) {
System.out.println("添加null时抛出异常:" + e);
}
}
}
ConcurrentLinkedQueue: [Aman, Amar]
要添加的Collection: null
添加null时抛出异常:java.lang.NullPointerException
示例4: 显示非法参数异常
// Java程序演示ConcurrentLinkedQueue的addAll()方法
import java.util.concurrent.*;
import java.util.*;
public class GFG {
public static void main(String[] args)
{
// 创建一个ConcurrentLinkedQueue
ConcurrentLinkedQueue<String>
queue = new ConcurrentLinkedQueue<String>();
// 将字符串添加到队列中
queue.add("Aman");
queue.add("Amar");
// 显示现有的ConcurrentLinkedQueue
System.out.println("ConcurrentLinkedQueue: " + queue);
try {
// 应用addAll()方法并将队列作为参数传递
boolean response = queue.addAll(queue);
}
catch (IllegalArgumentException e) {
System.out.println("在需要集合时将队列添加到本身时抛出异常:" + e);
}
}
}
ConcurrentLinkedQueue: [Aman, Amar]
在需要集合时将队列添加到本身时抛出异常:java.lang.IllegalArgumentException
参考链接: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentLinkedQueue.html#addAll-java.util.Collection-
极客教程