Last Updated : 29 Nov, 2018
The
addAll(Collection collection)of
java.util.Collection interfaceis 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
collectionof type Collection which is to be added to this collection.
Return Value:This method returns a
boolean valuedepicting the successfulness of the operation. If the collection was added, it returns true, else it returns false.
Exceptions:This method throws following exceptions:
Below examples illustrate the Collection addAll() method:
Example 1:Using LinkedList Class
Java
// 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);
}
}
Output:
The LinkedList is: [] The new linked list is: [A, Computer, Portal, for, Geeks]Example 2:
Using ArrayDeque Class
Java
// 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);
}
}
Output:
The ArrayDeque is: [] The new ArrayDeque is: [Welcome, To, Geeks, 4, Geeks]Example 3:
Using ArrayList Class
Java
// 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);
}
}
Output:
The ArrayList is: [] The new ArrayList is: [A, Computer, Portal, for, Geeks]Example 4:
To demonstrate NullPointer Exception
Java
// 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);
}
}
}
Output:
The ArrayList is: [] Exception: java.lang.NullPointerExceptionReference: https://docs.oracle.com/javase/9/docs/api/java/util/Collection.html#addAll-java.util.Collection-
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4