Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

communitycreator
java

What is the Deque.isEmpty() 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 how to use the Deque.isEmpty() method in Java.

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

Deque.isEmpty() is used to check whether the deque is empty or not.

Syntax

boolean isEmpty()

Parameters

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

Return value

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

  • True: When the deque is empty.
  • False: When the deque is not empty.

Code

Let us look at the code snippet below.

import java.util.*;
class Main
{
public static void main(String[] args)
{
// Create the integer list
Deque<Integer> d = new LinkedList<Integer>();
// add elements in the list
d.add(2);
d.add(28);
d.add(6);
d.add(80);
d.add(21);
d.add(7);
d.add(15);
// check list isempty or not
if(d.isEmpty())
System.out.println("Deque is empty.");
else
System.out.println("Deque is not empty.");
}
}
Using the Deque.isEmpty() method in Java

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 that consists of Integer type elements.
  • From lines 8 to 14, we use the Deque.add() method to insert the elements in the deque.
  • In lines 16 to 19, we check whether the deque is empty or not and display the message accordingly.

In this way, we can use the Deque.isEmpty() 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