The Wayback Machine - https://web.archive.org/web/20240618020217/https://www.geeksforgeeks.org/arraydeque-offer-method-in-java/
Open In App

ArrayDeque offer() Method in Java

Last Updated : 10 Dec, 2018
Improve
Improve
Like Article
Like
Save
Share
Report

The Java.util.ArrayDeque.offer(Object element) method in Java is used to add a specific element at the end of the Deque. The function is similar to the offerLast() method of ArrayDeque in Java.

Syntax:

Array_Deque.offer(Object element)

Parameters: The parameter element is of the type ArrayDeque and refers to the element to be added at the end of the Deque.

Return Value: The function returns True if the element is successfully added into the deque else it returns false.

Exceptions: The method throws NullPointerException if the passed parameter is NULL.

Below programs illustrate the Java.util.ArrayDeque.offer() method:
Program 1: Adding String elements into the Deque.




// Java code to illustrate offer()
import java.util.*;
  
public class ArrayDequeDemo {
    public static void main(String args[])
    {
        // Creating an empty ArrayDeque
        Deque<String> de_que = new ArrayDeque<String>();
  
        // Use add() method to add elements into the Deque
        de_que.add("Welcome");
        de_que.add("To");
        de_que.add("Geeks");
        de_que.add("4");
        de_que.add("Geeks");
  
        // Displaying the ArrayDeque
        System.out.println("Initial Deque: " + de_que);
  
        // Using offer() to add elements
        de_que.offer("Hello");
        de_que.offer("World");
  
        // Displaying the ArrayDeque
        System.out.println("Final Deque: " + de_que);
    }
}


Output:

Initial Deque: [Welcome, To, Geeks, 4, Geeks]
Final Deque: [Welcome, To, Geeks, 4, Geeks, Hello, World]

Program 2: Adding Integer elements into the Deque.




// Java code to illustrate offer()
import java.util.*;
  
public class ArrayDequeDemo {
    public static void main(String args[])
    {
        // Creating an empty ArrayDeque
        Deque<Integer> de_que = new ArrayDeque<Integer>();
  
        // Use add() method to add elements into the Deque
        de_que.add(10);
        de_que.add(15);
        de_que.add(30);
        de_que.add(20);
        de_que.add(5);
  
        // Displaying the ArrayDeque
        System.out.println("Initial Deque: " + de_que);
  
        // Using offer() to add elements
        de_que.offer(1658);
        de_que.offer(2458);
  
        // Displaying the ArrayDeque
        System.out.println("Final Deque: " + de_que);
    }
}


Output:

Initial Deque: [10, 15, 30, 20, 5]
Final Deque: [10, 15, 30, 20, 5, 1658, 2458]


Previous Article
Next Article

Similar Reads

Java 8 | ArrayDeque removeIf() method in Java with Examples
The removeIf() method of ArrayDeque is used to remove all those elements from ArrayDeque which satisfies a given predicate filter condition passed as a parameter to the method. This method returns true if some element are removed from the Vector. Java 8 has an important in-built functional interface which is Predicate. Predicate, or a condition che
3 min read
Java.util.LinkedList.offer(), offerFirst(), offerLast() in Java
Linked list also has a function that does the work of flexible addition of elements and helps addition both at front and back of the list, these functions literally "offer" the facility and named offer(). Three types are available and are discussed in this very article below. 1. offer(E e) : This method adds the specified element as the tail (last
4 min read
Java.util.ArrayDeque Class in Java | Set 1
java.util.ArrayDeque class describes an implementation of a resizable array structure which implements the Deque interface. Array deques has not immutable and can grow as necessary. They are not thread-safe and hence concurrent access by multiple threads is not supported unless explicitly synchronized. Null elements are invalid in this structure. M
5 min read
Java.util.ArrayDeque Class in Java | Set 2
Java.util.ArrayDeque Class in Java | Set 1 More Methods of util.ArrayDeque Class : 16. offer(Element e) : java.util.ArrayDeque.offer(Element e) : inserts element at the end of deque. Syntax : public boolean offer(Element e) Parameters : e - element to add Return : true, if element is added else; false 17. offerFirst(Element e) : java.util.ArrayDequ
5 min read
LinkedTransferQueue offer() method in Java
offer(E e, long timeout, TimeUnit unit) The offer(E e, long timeout, TimeUnit unit) method of java.util.concurrent.LinkedTransferQueue Class is an in-built function in Java which inserts the element passed as parameter to method at the tail of this queue, if queue is not full. It will wait till a specified time for space to become available if the
4 min read
PriorityQueue offer() Method in Java
The java.util.PriorityQueue.offer() method is used to insert a particular element into the Priority Queue. It acts similar to the add() method of Priority Queue. Syntax: Priority_Queue.offer(Object element) Parameters: The parameter element is of the type PriorityQueue and refers to the element to be inserted into the Queue. Return Value: The metho
2 min read
ArrayBlockingQueue offer() Method in Java
ArrayBlockingQueue is bounded, blocking queue that stores the elements internally backed by an array. ArrayBlockingQueue class is a member of the Java Collections Framework. Bounded means it will have a fixed size, you can not store number the elements more than the capacity of the queue. The queue also follows FIFO (first-in-first-out) rule for st
6 min read
LinkedBlockingQueue | offer() Method in JAVA
There is two types of offer() method for LinkedBlockingQueue class : offer(E e, long timeout, TimeUnit unit) The offer(E e, long timeout, TimeUnit unit) method of LinkedBlockingQueue inserts the element passed as parameter to method at the tail of this LinkedBlockingQueue if queue is not full. It will wait till a specified time for space to become
6 min read
LinkedBlockingDeque offer() method in Java
The offer(E e) method of LinkedBlockingDeque inserts the element passed in the parameter to the end of the Deque. If the container's capacity has exceeded, then it does not returns an exception as in case of add() and addFirst() function. Syntax: public boolean offer(E e) Parameters: This method accepts a mandatory parameter e which is the element
2 min read
PriorityBlockingQueue offer() method in Java
1. offer(E e) Method The offer(E e) method of PriorityBlockingQueue inserts the element e, passed as parameter, in this PriorityBlockingQueue.This method inserts the element into this PriorityBlockingQueue. Since the PriorityBlockingQueue is unbounded, this method will be never be blocked. Syntax: public boolean offer(E e) Parameter: This method ac
4 min read