What is the ArrayDeque.isEmpty method in Java?
An Deque interface.
ArrayDequecan be used as both a stack and a queue.Nullelements 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
ArrayDequeclass. -
In line 4, we created an
ArrayDequeobject with the namearrayDeque. This object doesn’t have any elements in it. -
In line 6, we called the
isEmptymethod on thearrayDequeobject. This method will returntruebecause thearrayDequeobject is empty. -
In line 8, we used the
add()method of thearrayDequeobject to add one element ("1") toarrayDeque. -
In line 10, we called the
isEmptymethod on thearrayDequeobject. This method will returnfalsebecause thearrayDequeobject contains one element.