Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

communitycreator
java

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

Sumit Sharma

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

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

The Deque.add() method is used to insert the element at the end of the deque if the deque is not full. Otherwise, it throws an IllegalStateException.

Syntax

boolean add(element)
Syntax

Parameter

The Deque.add() method takes one parameter:

  • Element: Element to be inserted.

Return

The Deque.add() 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);
System.out.println("Elements in the deque are: "+d);
}
}
Using Deque.add() method in Java

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 displayed the deque elements.

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

RELATED TAGS

communitycreator
java

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring