Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
communitycreator

What is the Deque.size() 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.size() method in Java, which is present in the Deque interface inside the java.util package.

The Deque.size() method is used to obtain the number of elements present in the deque.

Syntax

x size() 

Parameter

The Deque.size() method doesn’t take any parameters.

Return

The Deque.size() method returns size, which is the number of elements present in the Deque.

Code

Let’s look at the code snippet below.

import java.util.*;
class Main
{
public static void main(String[] args)
{
Deque<Integer> d = new LinkedList<Integer>();
//Adding elements
d.add(212);
d.add(621);
d.add(23);
d.add(1280);
d.add(1171);
System.out.println("Elements in the deque are: "+d);
//Displaying the size of deque
System.out.println("Number of elements in the deque are: "+d.size());
}
}
Using the Deque.size() 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 line 8 to 12, we inserted values in the deque by using the Deque.add() method.
  • In line 14, we displayed the original deque elements.
  • In line 16, we used the Deque.size() method and displayed the number of elements present in the deque.

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

RELATED TAGS

java
communitycreator

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