Collection addAll() method in Java with Examples - GeeksforGeeks

29 Nov, 2018
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
// 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<String>            list = new LinkedList<String>();          // A collection is created        Collection<String>            collect = new LinkedList<String>();        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]

Example 2: Using ArrayDeque Class
// Java code to illustrate addAll() method  import java.util.*;  public class ArrayDequeDemo {    public static void main(String args[])    {        // Creating an empty ArrayDeque        Collection<String>            de_que = new ArrayDeque<String>();          // Creating a new ArrayDeque        Collection<String>            deque = new ArrayDeque<String>();        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]

Example 3: Using ArrayList Class
// Java code to illustrate boolean addAll()  import java.util.*;  public class LinkedListDemo {    public static void main(String args[])    {          // Creating an empty ArrayList        Collection<String>            list = new ArrayList<String>();          // A collection is created        Collection<String>            collect = new ArrayList<String>();        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]

Example 4: To demonstrate NullPointer Exception
// Java code to illustrate boolean addAll()  import java.util.*;  public class LinkedListDemo {    public static void main(String args[])    {          // Creating an empty ArrayList        Collection<String>            list = new ArrayList<String>();          // A collection is created        Collection<String> 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

Reference: https://docs.oracle.com/javase/9/docs/api/java/util/Collection.html#addAll-java.util.Collection-
Java - util package
Java-Collections
Java-Functions
Java
Java
Java-Collections
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Stream In Java
Different ways of Reading a text file in Java
Constructors in Java
Exceptions in Java
Generics in Java
Functional Interfaces in Java
Comparator Interface in Java with Examples
HashMap get() Method in Java
Introduction to Java
Difference between Abstract Class and Interface in Java