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 addLast(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.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program Demonstrate addAll()
// method of ArrayDeque
  
import java.util.*;
public class GFG {
  
    public static void main(String[] args)
    {
  
        // create an ArrayDeque which going to
        // contains a list of Numbers
        ArrayDeque<Integer> Numbers = new ArrayDeque<Integer>();
  
        // Add Number to list
        Numbers.add(23);
        Numbers.add(32);
        Numbers.add(45);
        Numbers.add(63);
  
        // print ArrayDeque before calling addAll()
        System.out.println("Before calling addAll()");
        print(Numbers);
  
        // create a collection of Number to
        // add in ArrayDeque using addAll()
        ArrayList<Integer> col = new ArrayList<Integer>();
  
        // add Numbers in collection
        col.add(74);
        col.add(65);
        col.add(84);
  
        // add the elements of collection at the end
        // of ArrayDeque using addAll()
        Numbers.addAll(col);
  
        // print ArrayDeque
        System.out.println("After calling addAll()");
        print(Numbers);
    }
  
    // printing all elements of ArrayDeque
    public static void print(ArrayDeque<Integer> arDe)
    {
  
        arDe.forEach((n) -> System.out.print(n + " "));
  
        System.out.println();
    }
}

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program Demonstrate addAll()
// method of ArrayDeque
  
import java.util.*;
public class GFG {
  
    public static void main(String[] args)
    {
        // create an ArrayDeque which going to
        // contains a list of Student names which is actually
        // string values
        ArrayDeque<String> students = new ArrayDeque<String>();
  
        // Add Strings to list
        // each string represents student name
        students.add("Ram");
        students.add("Mohan");
        students.add("Sohan");
        students.add("Rabi");
  
        // print ArrayDeque before calling addAll()
        System.out.println("Before calling addAll()");
        print(students);
  
        // create a collection of String to
        // add in ArrayDeque using addAll()
        LinkedList<String> col = new LinkedList<String>();
  
        // add Names in collection
        col.add("Rohan");
        col.add("Kartik");
  
        // add the elements of collection at the end
        // of ArrayDeque using addAll()
        students.addAll(col);
  
        // print ArrayDeque
        System.out.println("After calling addAll()");
        print(students);
    }
  
    // printing all elements of ArrayDeque
    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");
    }
}

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program Demonstrate Exception thrown by addAll()
// method of ArrayDeque
  
import java.util.*;
public class GFG {
  
    public static void main(String[] args)
    {
  
        // create an ArrayDeque which going to
        // contains a list of Numbers
        ArrayDeque<Integer> Numbers = new ArrayDeque<Integer>();
  
        // Add Number to list
        Numbers.add(223);
        Numbers.add(132);
        Numbers.add(345);
        Numbers.add(563);
  
        // create a collection of Number which is null
        ArrayList<Integer> col = null;
  
        try {
            // call addAll() method
            Numbers.addAll(col);
        }
        catch (Exception e) {
            System.out.println(e);
        }
    }
}

chevron_right


Output:

java.lang.NullPointerException

Reference: https://docs.oracle.com/javase/10/docs/api/java/util/ArrayDeque.html#addAll(java.util.Collection)



My Personal Notes arrow_drop_up

I am a Developer I love to code and bring my ideas alive

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.