Java Deque add() addFirst() addLast() Tutorial

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

What is Java Deque add() Method?

The Java Deque add() method is used to add an element to a Deque object.

Note: using the add() method will add an element to the tail (end) of a Deque object.

Java add() Method Syntax:

boolean add(E e)

add() Method Parameters:

The method takes one argument and that is the element we want to add to a Deque object.

add() Method Return Value:

The return value of this method is of boolean type and is equal to true if the specified element was added to the end of the target Deque object or false if the operation failed and no element was added.

add() Method Exceptions:

The method might throw four exceptions:

IllegalStateException – We get this exception if the target Deque object is full and we try to add another value.

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

NullPointerException – We get this exception if the target Deque object does not allow a null value to be added.

IllegalArgumentException – This exception will be thrown if the specified element has a property that won’t allow it to be an element of a Deque object.

Example: using Deque add() 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);
        }
    }
        
}

Output:

1

2

3

0

10

What is Java Deque addFirst() Method?

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

For example, you might have a Deque object with these elements: 1,2,3,4,5

Now if you call this method to a value like 10, then the Deque object will have: 10, 1, 2, 3, 4, 5

Java addFirst() Method Syntax:

void addFirst(E e)

addFirst() Method Parameters:

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

addFirst() Method Return Value:

The return value of this method is void.

addFirst() Method Exceptions:

IllegalStateException – We get this exception if the target Deque object is full and we try to add another value.

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

NullPointerException – We get this exception if the target Deque object does not allow a null value to be added.

IllegalArgumentException – This exception will be thrown if the specified element has a property that won’t allow it to be an element of a Deque object.

Example: using Deque addFirst() 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);

        deque.addFirst(0);
        deque.addFirst(-1);
        deque.addFirst(-2);
        for (int i : deque){
            System.out.println(i);
        }
    }
        
}

Output:

-2

-1

0

1

2

3

0

10

What is Java Deque addLast() Method?

The Java Deque addLast() method is used to add an element to the end (tail) of a Deque object.

Note: this method does the same as the add() method.

Java addLast() Method Syntax:

void addLast(E e)

addLast() Method Parameters:

The method takes one argument and that is the element we want to add to the end of a Deque object.

addLast() Method Return Value:

The return value of this method is void.

addLast() Method Exceptions:

IllegalStateException – We get this exception if the target Deque object is full and we try to add another value.

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

NullPointerException – We get this exception if the target Deque object does not allow a null value to be added.

IllegalArgumentException – This exception will be thrown if the specified element has a property that won’t allow it to be an element of a Deque object.

Example: using Deque addLast() 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);

        deque.addLast(0);
        deque.addLast(-1);
        deque.addLast(-2);
        for (int i : deque){
            System.out.println(i);
        }
    }
        
}

Output:

1

2

3

0

10

0

-1

-2
Facebook
Twitter
Pinterest
LinkedIn

Top Technologies