What is the Deque.addAll() method in Java?

In this shot, we will discuss the Deque.addAll() method, which is present in the Deque interface inside the java.util package.

Deque.addAll() is used to insert the collection of elements at the end of the deque if the deque is not full.

Syntax

boolean addLast(Collection <E>)

Parameter

The Deque.addAll() method takes one parameter:

  • Collection: Collection of elements to be inserted.

Return

The Deque.addAll() method returns a boolean value:

  • True: When the element is inserted successfully in the deque.
  • False: When the element is not inserted successfully in the deque.

Code

Let’s take a look at the below code snippet.

import java.util.*;
class Main
{
public static void main(String[] args)
{
Deque<Integer> d = new LinkedList<Integer>();
d.add(212);
d.add(23);
d.add(621);
d.add(1280);
Deque<Integer> q = new LinkedList<Integer>();
q.add(3121);
q.add(18297);
q.add(791);
d.addAll(q);
System.out.println("Elements in the deque are: "+d);
}
}

Explanation

  • In line 1, we imported the required package.
  • In line 2, we made a Main class.
  • In line 4, we made a main function.
  • In line 6, we declared a deque consisting of Integer type elements.
  • From lines 8 to 11, we inserted the elements in a deque by using the Deque.add() method.
  • In line 13, we declared another deque consisting of Integer type elements.
  • From lines 14 to 16, we inserted the elements in another deque by using the Deque.add() method.
  • In line 18, we added the other deque elements collection into our first deque using the Deque.addAll() method.

In this way, we can use the Deque.addAll() method in Java.

Free Resources