What is the ArrayDeque.isEmpty method in Java?

An ArrayDequeArray Double Ended Queue is a growable array that allows us to add or remove an element from both the front and back. This class is the implementation of the Deque interface.

ArrayDeque can be used as both a stack and a queue. Null elements are prohibited.

The isEmpty method

The isEmpty method can be used to check if the ArrayDeque object has no elements.

Syntax

public boolean isEmpty()

Parameters

This method does not take any arguments.

Return value

This method returns true if the arrayDeque object has no elements. Otherwise, it returns false.

Code

The code below demonstrates how to use the isEmpty method.

import java.util.ArrayDeque;
class IsEmpty{
public static void main( String args[] ) {
ArrayDeque<String> arrayDeque = new ArrayDeque<>();
System.out.println("the arrayDeque is : " + arrayDeque);
System.out.println("Check if the arrayDeque is empty : " + arrayDeque.isEmpty());
arrayDeque.add("1");
System.out.println("\nthe arrayDeque is : " + arrayDeque);
System.out.println("Check if the arrayDeque is empty : " + arrayDeque.isEmpty());
}
}

Explanation

In the code above:

  • In line 1, we imported the ArrayDeque class.

  • In line 4, we created an ArrayDeque object with the name arrayDeque. This object doesn’t have any elements in it.

  • In line 6, we called the isEmpty method on the arrayDeque object. This method will return true because the arrayDeque object is empty.

  • In line 8, we used the add() method of the arrayDeque object to add one element ("1") to arrayDeque.

  • In line 10, we called the isEmpty method on the arrayDeque object. This method will return false because the arrayDeque object contains one element.

Free Resources