Java ArrayDeque addAll() Method



Description

The Java ArrayDeque addAll​(Collection<? extends E> c) method inserts all the elements the specified collection c at the end of the deque. This method is equivalent to calling addLast(i) for each item of the collection c. Insertion order is maintained while adding the collection to the ArrayDeque and we can check the same by printing each element one by one.

Declaration

Following is the declaration for java.util.ArrayDeque.addAll() method

public boolean addAll​(Collection<? extends E> c)

Parameters

c − The collection of elements to be added in the deque.

Return Value

This method returns true if deque is changed as a result of this call, otherwise it returns false.

Exception

NullPointerException − if the specified collection or any of its element is null.

Adding Multiple Elements to an ArrayDeque of Integers Example

The following example shows the usage of Java ArrayDeque addAll(collection) method to add a collection of Integers. We're adding couple of Integers to the ArrayDeque object using addAll(collection) method in single statement and then print each element to show the elements added.

package com.tutorialspoint;

import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Deque;

public class ArrayDequeDemo {
   public static void main(String[] args) {

      // create an empty array deque
      Deque<Integer> deque = new ArrayDeque<>();

      // use addAll() method to add multiple elements in the deque
      deque.addAll(Arrays.asList(20,30,20,30,15,22,11));
      System.out.println("ArrayDeque = " + deque);      
   }
}

Output

Let us compile and run the above program, this will produce the following result −

ArrayDeque = [20, 30, 20, 30, 15, 22, 11]

Adding Multiple Elements to an ArrayDeque of Strings Example

The following example shows the usage of Java ArrayDeque addAll(collection) method to add a collection of Strings. We're adding couple of strings to the ArrayDeque object using addAll(collection) method using single statement and then printing the ArrayDeque using its toString() method.

package com.tutorialspoint;

import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Deque;

public class ArrayDequeDemo {
   public static void main(String[] args) {

      // create an empty array deque
      Deque<String> deque = new ArrayDeque<>();

      // use addAll() method to add multiple elements in the deque
      deque.addAll(Arrays.asList("Welcome","To","Tutorialspoint"));
      System.out.println("ArrayDeque = " + deque);      
   }
}

Output

Let us compile and run the above program, this will produce the following result −

ArrayDeque = [Welcome, To, Tutorialspoint]

Adding Multiple Elements to an ArrayDeque of Objects Example

The following example shows the usage of Java ArrayDeque addAll(collection) method to add a collection of Student objects. We're adding couple of Student objects to the ArrayDeque object using addAll(collection) method in a single statement and then printing the ArrayDeque using its toString() method.

package com.tutorialspoint;

import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Deque;
import java.util.List;

public class ArrayDequeDemo {
   public static void main(String[] args) {

      // create an empty array deque
      Deque<Student> deque = new ArrayDeque<Student>();

      List<Student> students = Arrays.asList(new Student(1, "Julie"),
         new Student(2, "Robert"), new Student(3, "Adam"));

      // use addAll() method to add multiple elements in the deque
      deque.addAll(students);
      System.out.println("ArrayDeque = " + deque);      
   }
}

class Student {
   int rollNo;
   String name;

   Student(int rollNo, String name){
      this.rollNo = rollNo;
      this.name = name;
   }

   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + " ]";
   }
}

Output

Let us compile and run the above program, this will produce the following result −

ArrayDeque = [[ 1, Julie ], [ 2, Robert ], [ 3, Adam ]]
java_util_arraydeque.htm
Advertisements