Java Deque size() Method Tutorial

In this section, we will learn what the Deque size() method is and how to use it in Java.

What is Java Deque size() Method?

The Java Deque size() method is used to get the size of a Deque object.

When we say “size”, that means the number of elements that are currently in the target Deque object.

Java size() Method Syntax:

int size()

size() Method Parameters:

The method does not take an argument.

size() Method Return Value:

The return value of this method is of type Integer and it is equal to the number of elements that are currently in the target Deque object.

size() Method Exceptions:

The method does not throw an exception.

Example: using Deque size() method

import java.util.Deque; 
import java.util.ArrayDeque;
class Main{
    public static void main(String[] args) {
        Deque<Integer> dq = new ArrayDeque<>();
        dq.addLast(1);
        dq.addLast(2);
        dq.addLast(3);
        dq.addLast(0);
        dq.addLast(10);
        dq.addLast(30);

        System.out.println(dq.size());
    }
}

Output:

6

Leave a Reply