Deque offerFirst() method in Java
The offerFirst(E e) method of Deque Interface inserts the specified element into the front of the Deque if it is possible to do so immediately without violating capacity restrictions. This method is preferable to addFirst() method since this method does not throws an exception when the capacity of the container is full since it returns false.
Syntax:
boolean offerFirst(E e)
Parameters: This method accepts a mandatory parameter e which is the element to be inserted in the front of the Deque.
Returns: This method returns true on successful insertion else it returns false.
Exceptions: The function throws four exceptions which are described as below:
- ClassCastException: when the class of the element to be entered prevents it from being added to this container.
- IllegalArgumentException: when some property of the element prevents it to be added to the Deque.
- NullPointerException: when the element to be inserted is passed as null and the Deque’s interface does not allow null elements.
Below programs illustrate offerFirst() method of Deque:
Program 1:
// Java Program Demonstrate offerFirst() // method of Deque when Null is passed import java.util.*; import java.util.concurrent.LinkedBlockingDeque; public class GFG { public static void main(String[] args) throws IllegalStateException { // create object of Deque Deque<Integer> DQ = new LinkedBlockingDeque<Integer>(3); if (DQ.offerFirst(10)) System.out.println("The Deque is not full and 10 is inserted"); else System.out.println("The Deque is full"); if (DQ.offerFirst(15)) System.out.println("The Deque is not full and 15 is inserted"); else System.out.println("The Deque is full"); if (DQ.offerFirst(25)) System.out.println("The Deque is not full and 25 is inserted"); else System.out.println("The Deque is full"); if (DQ.offerFirst(20)) System.out.println("The Deque is not full and 20 is inserted"); else System.out.println("The Deque is full"); // before removing print Deque System.out.println("Deque: " + DQ); } } |
The Deque is not full and 10 is inserted The Deque is not full and 15 is inserted The Deque is not full and 25 is inserted The Deque is full Deque: [25, 15, 10]
Program 2:
// Java Program Demonstrate offerFirst() // method of Queue when Null is passed import java.util.*; import java.util.concurrent.LinkedBlockingDeque; public class GFG { public static void main(String[] args) throws NullPointerException { // create object of Queue Deque<Integer> DQ = new LinkedBlockingDeque<Integer>(); // Add numbers to end of Deque DQ.offerFirst(7855642); DQ.offerFirst(35658786); DQ.offerFirst(5278367); // when null is inserted DQ.offerFirst(null); // before removing print Deque System.out.println("Deque: " + DQ); } } |
Output:
Exception in thread "main" java.lang.NullPointerException
at java.util.concurrent.LinkedBlockingDeque.offerFirst(LinkedBlockingDeque.java:342)
at GFG.main(GFG.java:21)
Note: The other two exceptions are internal and are caused depending on the compiler hence it cannot be shown in the code.
Reference: https://docs.oracle.com/javase/8/docs/api/java/util/Deque.html#offerFirst-E-
Recommended Posts:
- LinkedBlockingDeque offerFirst() method in Java
- ConcurrentLinkedDeque offerFirst() method in Java
- ArrayDeque offerFirst() Method in Java
- BlockingDeque offerFirst() method in Java with Examples
- Java.util.LinkedList.offer(), offerFirst(), offerLast() in Java
- Deque contains() method in Java
- Deque add() method in Java
- Deque getFirst() method in Java
- Deque getLast() method in Java
- Deque addLast() method in Java
- Deque descendingIterator() method in Java
- Deque offerLast() method in Java
- Deque offer() method in Java
- Deque iterator() method in Java
- Deque element() method in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



