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

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.");
}
}

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.