Java Deque offer() offerFirst() offerLast() Methods Tutorial

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

What is Java Deque offer() Method?

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

Note: the element we add using offer() method will be added to the tail of the target Deque object.

Java offer() Method Syntax:

boolean offer(E e)

offer() Method Parameters:

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

offer() Method Return Value:

The return value of this method is a boolean.

If the specified element was added to the target Deque object, then the return value of this method becomes true. Otherwise, the result will be false.

offer() Method Exceptions:

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 offer() method

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

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

Output:

1

2

3

0

10

What is Java Deque offerFirst() Method?

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

Note: the element we add using offerFirst() method will be added to the head of the target Deque object.

Java offerFirst() Method Syntax:

boolean offerFirst(E e)

offerFirst() Method Parameters:

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

offerFirst() Method Return Value:

The return value of this method is a boolean.

If the specified element was added to the target Deque object, then the return value of this method becomes true. Otherwise, the result will be false.

offerFirst() Method Exceptions:

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 offerFirst() method

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

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

Output:

10

0

3

2

1

What is Java Deque offerLast() Method?

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

Note: this method works the same as the offer() method.

Java offerLast() Method Syntax:

boolean offerLast(E e)

offerLast() Method Parameters:

The method takes one argument and that is the element we want to add to the end (tail) of the target Deque object.

offerLast() Method Return Value:

The return value of this method is a boolean.

If the specified element was added to the target Deque object, then the return value of this method becomes true. Otherwise, the result will be false.

offerLast() Method Exceptions:

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 offerLast() method

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

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

Output:

1

2

3

0

10
Facebook
Twitter
Pinterest
LinkedIn

Top Technologies