What is Deque.addLast() method in Java?

The Deque.addLast(element) method is present in the Deque interface inside the java.util package.

Deque.addLast(element) is used to insert the element at the end of the deque if the deque is not full.

Syntax

boolean addLast(element)

Parameter

The Deque.addLast(element) method takes one parameter:

  • element: This is an element to be inserted.

Return

The Deque.addLast(element) method returns a Boolean value:

  • true: When the element is inserted successfully in the deque.
  • false: When the element is not inserted in the deque.

Code

Let’s take a look at the code widget below:

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

Explanation

  • In line 1, we import the required package.
  • In line 2, we make a Main class.
  • In line 4, we make a main function.
  • In line 6, we declare a deque consisting of Integer type elements.
  • In lines 8-9, we insert the elements of deque by using the Deque.add() method.
  • In lines 10-11, we insert the elements at the end of the deque by using the Deque.addLast() method.
  • In line 13, we display the deque elements.

Free Resources