Deque offerFirst() method in Java Last Updated : 19 Sep, 2018 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 // 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); } } Output: 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 // 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- Comment More infoAdvertise with us Next Article Deque offerFirst() method in Java G gopaldave Follow Improve Article Tags : Misc Java Java-Collections Java - util package Java-Functions java-deque +2 More Practice Tags : JavaJava-CollectionsMisc Similar Reads Deque Interface in Java Deque Interface present in java.util package is a subtype of the queue interface. The Deque is related to the double-ended queue that supports adding or removing elements from either end of the data structure. It can either be used as a queue(first-in-first-out/FIFO) or as a stack(last-in-first-out/ 9 min read Deque add() method in Java The add(E e) method of Deque Interface inserts the element passed in the parameter to the end of the Deque if there is space. If the Deque is capacity restricted and no space is left for insertion, it returns an IllegalStateException. The function returns true on successful insertion. Syntax:  bool 4 min read Deque addFirst() method in Java with Examples The addFirst(E e) method of Deque Interface inserts the element passed in the parameter to the front of the Deque if there is space. If the Deque is capacity restricted and no space is left for insertion, it returns an IllegalStateException. The function returns true on successful insertion. Syntax: 4 min read Deque addLast() method in Java The addLast(E e) method of Deque Interface inserts the element passed in the parameter to the end of the Deque if there is space. If the Deque is capacity restricted and no space is left for insertion, it returns an IllegalStateException. The function returns true on successful insertion. Syntax: bo 4 min read Deque contains() method in Java The contains(E e) method of Deque Interface check for the presence of the element e in the Deque container. If the Deque contains one occurrence of the element, then it returns true else it returns false. Syntax: boolean contains(Object o) Parameters: This method accepts a mandatory parameter o whic 5 min read Deque descendingIterator() Method in Java The descendingIterator() method of the Deque Interface returns an iterator over the elements in this deque in reverse order. The elements will be returned from the last to the first order. This can be used with any class implementing the Deque Interface like LinkedList, ArrayDeque, LinkedBlockingDeq 2 min read Deque element() method in Java The element() method of Deque Interface returns the element at the front the container. It does not deletes the element in the container. This method returns the head of the Deque. The method throws an exception when the Deque is empty. Syntax:  E element() Parameters: This method does not accepts 3 min read Deque getFirst() method in Java The getFirst() method of Deque Interface returns the first element or the head of the Deque. It does not deletes the element. It throws an exception when the Deque is empty. Syntax:  E getFirst() Parameters: This method does not accepts any parameter.Returns: This method returns the first element o 3 min read Deque getLast() method in Java The getLast() method of Deque Interface returns the last element or the tail of the Deque. It does not deletes the element. It throws an exception when the Deque is empty. Syntax: E getLast() Parameters: This method does not accepts any parameter. Returns: This method returns the last element or the 3 min read Deque iterator() method in Java The iterator() method of Deque Interface returns an iterator over the elements in this deque in a proper sequence. The elements will be returned in order from first (head) to last (tail). The returned iterator is a âweakly consistentâ iterator. Syntax: Iterator iterator() Parameters: This method doe 3 min read Like