Java Deque pop() push() Methods Tutorial

In this section, we will learn what the Deque pop() and push() methods are and how to use them in Java.

What is Java Deque pop() Method?

The Java Deque pop() method is used to pop (remove) an element from the head of a Deque object.

Note: this method will return the removed element as a result.

Java pop() Method Syntax:

E pop()

pop() Method Parameters:

The method does not take an argument.

pop() Method Return Value:

The return value of this method is the element that was removed from the head of the target Deque object as a result of calling this method.

pop() Method Exceptions:

The method might throw one exception and that is:

NoSuchElementException: We get this exception if the target Deque object was in fact empty.

Example: using Deque pop() method

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

        for (int i: deque){
            System.out.println(i);
        }
        System.out.println("Calling the pop method two times: ");
        System.out.println(deque.pop());
        System.out.println(deque.pop());

        System.out.println("The size of the Deque object after poping two elements: "+ deque.size());
    }
        
}

Output:

1

2

3

0

10

Calling the pop method two times:

1

2

The size of the Deque object after poping two elements: 3

What is Java Deque push() Method?

The Java Deque push() method is used to add an element to the head (beginning) of a Deque object.

Java push() Method Syntax:

void push(E e)

push() Method Parameters:

The method takes one argument and that is the element we want to add to the head (beginning) of a Deque object.

push() Method Return Value:

The return value of this method is void.

push() Method Exceptions:

The method might throw four exceptions:

IllegalStateException: We get this exception if the specified element could not be added to the target Deque object because of its capacity is full.

ClassCastException – We get this exception if the argument datatype is of incompatible type compared to the elements of the target Deque object.

NullPointerException – This exception will be thrown if we put a null value as the argument and the target Deque object but it doesn’t permit such value.

IllegalArgumentException – we get this exception if the argument had a property that prevents it from being added to the target Deque object.

Example: using Deque push() method

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

        for (int i: deque){
            System.out.println(i);
        }
    }
        
}

Output:

10

0

3

2

1
Facebook
Twitter
Pinterest
LinkedIn

Top Technologies