用示例收集Java中的 addAll() 方法
addAll(集合
句法:
Collection.addAll(Collection collection)
参数:此方法接受要添加到此集合的 Collection 类型的强制参数集合。
返回值:此方法返回一个布尔值,表示操作是否成功。如果添加了集合,则返回 true,否则返回 false。
异常:此方法引发以下异常:
- UnsupportedOperationException:如果此集合不支持添加操作
- ClassCastException:如果指定元素的类阻止它被添加到此集合中
- NullPointerException:如果指定元素为空且此集合不允许空元素
- IllegalArgumentException:如果元素的某些属性阻止它被添加到此集合中
- IllegalStateException:如果此时由于插入限制无法添加元素
下面的示例说明了 Collection addAll() 方法:
示例 1:使用 LinkedList 类
// Java code to illustrate boolean addAll()
import java.util.*;
import java.util.*;
public class LinkedListDemo {
public static void main(String args[])
{
// Creating an empty LinkedList
Collection
list = new LinkedList();
// A collection is created
Collection
collect = new LinkedList();
collect.add("A");
collect.add("Computer");
collect.add("Portal");
collect.add("for");
collect.add("Geeks");
// Displaying the list
System.out.println("The LinkedList is: " + list);
// Appending the collection to the list
list.addAll(collect);
// displaying the modified LinkedList
System.out.println("The new linked list is: "
+ list);
}
}
输出:
The LinkedList is: []
The new linked list is: [A, Computer, Portal, for, Geeks]
示例 2:使用 ArrayDeque 类
// Java code to illustrate addAll() method
import java.util.*;
public class ArrayDequeDemo {
public static void main(String args[])
{
// Creating an empty ArrayDeque
Collection
de_que = new ArrayDeque();
// Creating a new ArrayDeque
Collection
deque = new ArrayDeque();
deque.add("Welcome");
deque.add("To");
deque.add("Geeks");
deque.add("4");
deque.add("Geeks");
// Displaying the list
System.out.println("The ArrayDeque is: " + de_que);
// Appending the collection to the list
de_que.addAll(deque);
// displaying the modified ArrayDeque
System.out.println("The new ArrayDeque is: "
+ de_que);
}
}
输出:
The ArrayDeque is: []
The new ArrayDeque is: [Welcome, To, Geeks, 4, Geeks]
示例 3:使用 ArrayList 类
// Java code to illustrate boolean addAll()
import java.util.*;
public class LinkedListDemo {
public static void main(String args[])
{
// Creating an empty ArrayList
Collection
list = new ArrayList();
// A collection is created
Collection
collect = new ArrayList();
collect.add("A");
collect.add("Computer");
collect.add("Portal");
collect.add("for");
collect.add("Geeks");
// Displaying the list
System.out.println("The ArrayList is: " + list);
// Appending the collection to the list
list.addAll(collect);
// displaying the modified ArrayList
System.out.println("The new ArrayList is: "
+ list);
}
}
输出:
The ArrayList is: []
The new ArrayList is: [A, Computer, Portal, for, Geeks]
示例 4:演示 NullPointer 异常
// Java code to illustrate boolean addAll()
import java.util.*;
public class LinkedListDemo {
public static void main(String args[])
{
// Creating an empty ArrayList
Collection
list = new ArrayList();
// A collection is created
Collection collect = null;
// Displaying the list
System.out.println("The ArrayList is: " + list);
try {
// Appending the collection to the list
list.addAll(collect);
}
catch (Exception e) {
System.out.println("Exception: " + e);
}
}
}
输出:
The ArrayList is: []
Exception: java.lang.NullPointerException
参考: https://docs.oracle.com/javase/9/docs/api/ Java/util/Collection.html#addAll-java.util.Collection-