The addAll(Collection 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
import java.util.*;
import java.util.*;
public class LinkedListDemo {
public static void main(String args[])
{
Collection<String>
list = new LinkedList<String>();
Collection<String>
collect = new LinkedList<String>();
collect.add( "A" );
collect.add( "Computer" );
collect.add( "Portal" );
collect.add( "for" );
collect.add( "Geeks" );
System.out.println( "The LinkedList is: " + list);
list.addAll(collect);
System.out.println( "The new linked list is: "
+ list);
}
}
|
Output:
The LinkedList is: []
The new linked list is: [A, Computer, Portal, for, Geeks]
Example 2: Using ArrayDeque Class
import java.util.*;
public class ArrayDequeDemo {
public static void main(String args[])
{
Collection<String>
de_que = new ArrayDeque<String>();
Collection<String>
deque = new ArrayDeque<String>();
deque.add( "Welcome" );
deque.add( "To" );
deque.add( "Geeks" );
deque.add( "4" );
deque.add( "Geeks" );
System.out.println( "The ArrayDeque is: " + de_que);
de_que.addAll(deque);
System.out.println( "The new ArrayDeque is: "
+ de_que);
}
}
|
Output:
The ArrayDeque is: []
The new ArrayDeque is: [Welcome, To, Geeks, 4, Geeks]
Example 3: Using ArrayList Class
import java.util.*;
public class LinkedListDemo {
public static void main(String args[])
{
Collection<String>
list = new ArrayList<String>();
Collection<String>
collect = new ArrayList<String>();
collect.add( "A" );
collect.add( "Computer" );
collect.add( "Portal" );
collect.add( "for" );
collect.add( "Geeks" );
System.out.println( "The ArrayList is: " + list);
list.addAll(collect);
System.out.println( "The new ArrayList is: "
+ list);
}
}
|
Output:
The ArrayList is: []
The new ArrayList is: [A, Computer, Portal, for, Geeks]
Example 4: To demonstrate NullPointer Exception
import java.util.*;
public class LinkedListDemo {
public static void main(String args[])
{
Collection<String>
list = new ArrayList<String>();
Collection<String> collect = null ;
System.out.println( "The ArrayList is: " + list);
try {
list.addAll(collect);
}
catch (Exception e) {
System.out.println( "Exception: " + e);
}
}
}
|
Output:
The ArrayList is: []
Exception: java.lang.NullPointerException
Reference: https://docs.oracle.com/javase/9/docs/api/java/util/Collection.html#addAll-java.util.Collection-