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
相关用法
- Java Set addAll()用法及代码示例
- Java AbstractSequentialList addAll()用法及代码示例
- Java AbstractQueue addAll()用法及代码示例
- Java Collections addAll()用法及代码示例
- Java Collection addAll()用法及代码示例
- Java SortedSet addAll()用法及代码示例
- Java AbstractList addAll()用法及代码示例
- Java AbstractCollection addAll()用法及代码示例
- Java CopyOnWriteArrayList addAll()用法及代码示例
- Java LinkedBlockingDeque addAll()用法及代码示例
- Java CopyOnWriteArraySet addAll()用法及代码示例
- Java List addAll()用法及代码示例
- Java ConcurrentLinkedDeque pop()用法及代码示例
- Java ConcurrentLinkedDeque contains()用法及代码示例
- Java ConcurrentLinkedDeque remove()用法及代码示例
注:本文由纯净天空筛选整理自JaniceShah大神的英文原创作品 ConcurrentLinkedDeque addAll() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。