ArrayDeque addAll() method in Java
The addAll() method of ArrayDeque is used to insert all the elements of the collection passed as parameter at the end of this ArrayDeque. For adding elements of a collection to ArrayDeque we have to iterate through the collection and add each element in ArrayDeque by using addAll(E e) method. This method works same as we discussed here but with less effort. This method returns True if this deque changed as a result of the calling this method.
Syntax:
public boolean addAll(Collection<? extends E> c)
Parameter: This method takes a parameter c which represents Collection of the elements we want to insert into this deque.
Returns: This method returns True if this deque changed as a result of the calling this method.
Exception: This method throws NullPointerException if the specified collection or any of its elements are null.
Below programs illustrate addAll() method of ArrayDeque:
Program 1: Program to demonstrate addAll() method on ArrayDeque which going add a collection containing Numbers.
Java
import java.util.*;
public class GFG {
public static void main(String[] args)
{
ArrayDeque<Integer> Numbers = new ArrayDeque<Integer>();
Numbers.add( 23 );
Numbers.add( 32 );
Numbers.add( 45 );
Numbers.add( 63 );
System.out.println("Before calling addAll()");
print(Numbers);
ArrayList<Integer> col = new ArrayList<Integer>();
col.add( 74 );
col.add( 65 );
col.add( 84 );
Numbers.addAll(col);
System.out.println("After calling addAll()");
print(Numbers);
}
public static void print(ArrayDeque<Integer> arDe)
{
arDe.forEach((n) -> System.out.print(n + " "));
System.out.println();
}
}
|
Output:
Before calling addAll()
23 32 45 63
After calling addAll()
23 32 45 63 74 65 84
Program 2: Program to demonstrate addAll() method on ArrayDeque which going to add a collection of Students Names.
Java
import java.util.*;
public class GFG {
public static void main(String[] args)
{
ArrayDeque<String> students = new ArrayDeque<String>();
students.add("Ram");
students.add("Mohan");
students.add("Sohan");
students.add("Rabi");
System.out.println("Before calling addAll()");
print(students);
LinkedList<String> col = new LinkedList<String>();
col.add("Rohan");
col.add("Kartik");
students.addAll(col);
System.out.println("After calling addAll()");
print(students);
}
public static void print(ArrayDeque<String> arDe)
{
System.out.println("List of Students Name:");
arDe.forEach((n) -> System.out.print(" | " + n + " | "));
System.out.println("\n");
}
}
|
Output:
Before calling addAll()
List of Students Name:
| Ram | | Mohan | | Sohan | | Rabi |
After calling addAll()
List of Students Name:
| Ram | | Mohan | | Sohan | | Rabi | | Rohan | | Kartik |
Program 3: Program to demonstrate Exception thrown by addAll() method.
Java
import java.util.*;
public class GFG {
public static void main(String[] args)
{
ArrayDeque<Integer> Numbers = new ArrayDeque<Integer>();
Numbers.add( 223 );
Numbers.add( 132 );
Numbers.add( 345 );
Numbers.add( 563 );
ArrayList<Integer> col = null ;
try {
Numbers.addAll(col);
}
catch (Exception e) {
System.out.println(e);
}
}
}
|
Output:
java.lang.NullPointerException
Reference: https://docs.oracle.com/javase/10/docs/api/java/util/ArrayDeque.html#addAll(java.util.Collection)
Last Updated :
10 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...