The
addAll(Collection of
java.util.Collection interface is used to add the Collection 'collection' to this existing collection. This method returns a boolean value depicting the successfulness of the operation. If the collection was added, it returns true, else it returns false.
Syntax:
Collection.addAll(Collection<E> collection)
Parameters: This method accepts a mandatory parameter
collection of type Collection which is to be added to this collection.
Return Value: This method returns a
boolean value depicting the successfulness of the operation. If the collection was added, it returns true, else it returns false.
Exceptions: This method throws following exceptions:
- UnsupportedOperationException: if the add operation is not supported by this collection
- ClassCastException: if the class of the specified element prevents it from being added to this collection
- NullPointerException: if the specified element is null and this collection does not permit null elements
- IllegalArgumentException: if some property of the element prevents it from being added to this collection
- IllegalStateException: if the element cannot be added at this time due to insertion restrictions
Below examples illustrate the Collection addAll() method:
Example 1: Using LinkedList Class
Output:
The LinkedList is: []
The new linked list is: [A, Computer, Portal, for, Geeks]
Example 2: Using ArrayDeque Class
Output:
The ArrayDeque is: []
The new ArrayDeque is: [Welcome, To, Geeks, 4, Geeks]
Example 3: Using ArrayList Class
Output:
The ArrayList is: []
The new ArrayList is: [A, Computer, Portal, for, Geeks]
Example 4: To demonstrate NullPointer Exception