BlockingDeque offerFirst() method in Java with Examples Last Updated : 25 Jan, 2021 Summarize Comments Improve Suggest changes Share Like Article Like Report The offerFirst(E e) method of BlockingDeque inserts the element passed in the parameter at the front of the Deque container. 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 offerFirst(E e) Parameters: This method accepts a mandatory parameter e which is the element to be inserted at the front of the BlockingDeque. Returns: This method returns true if the element has been inserted, else it returns false. Note: The offerFirst() method of BlockingDeque has been inherited from the LinkedBlockingDeque class in Java. Below programs illustrate offerFirst() method of LinkedBlockingDeque: Program 1: Java // Java Program Demonstrate offerFirst() // method of BlockingDeque import java.util.concurrent.LinkedBlockingDeque; import java.util.concurrent.BlockingDeque; import java.util.*; public class GFG { public static void main(String[] args) throws IllegalStateException { // create object of BlockingDeque BlockingDeque<Integer> BD = new LinkedBlockingDeque<Integer>(4); // Add numbers to end of BlockingDeque BD.offerFirst(7855642); BD.offerFirst(35658786); BD.offerFirst(5278367); BD.offerFirst(74381793); // Cannot be inserted BD.offerFirst(10); // cannot be inserted hence returns false if (!BD.offerFirst(10)) System.out.println("The element 10 cannot be inserted" + " as capacity is full"); // before removing print queue System.out.println("Blocking Deque: " + BD); } } Output: The element 10 cannot be inserted as capacity is full Blocking Deque: [74381793, 5278367, 35658786, 7855642] Program 2: Java // Java Program Demonstrate offerFirst() // method of BlockingDeque import java.util.concurrent.LinkedBlockingDeque; import java.util.concurrent.BlockingDeque; import java.util.*; public class GFG { public static void main(String[] args) throws IllegalStateException { // create object of BlockingDeque BlockingDeque<String> BD = new LinkedBlockingDeque<String>(4); // Add numbers to end of BlockingDeque BD.offerFirst("abc"); BD.offerFirst("gopu"); BD.offerFirst("geeks"); BD.offerFirst("richik"); // Cannot be inserted BD.offerFirst("hii"); // cannot be inserted hence returns false if (!BD.offerFirst("hii")) System.out.println("The element 'hii' cannot be" + " inserted as capacity is full"); // before removing print queue System.out.println("Blocking Deque: " + BD); } } Output: The element 'hii' cannot be inserted as capacity is full Blocking Deque: [richik, geeks, gopu, abc] Reference: https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/BlockingDeque.html#offerFirst(E) Comment More infoAdvertise with us Next Article BlockingDeque takeFirst() method in Java with Examples G gopaldave Follow Improve Article Tags : Java Java-Collections Java-Functions java-interfaces Practice Tags : JavaJava-Collections Similar Reads BlockingDeque offerLast() method in Java with examples The offerLast(E e) method of BlockingDeque inserts the element passed in the parameter at the end of the Deque container. If the container's capacity has exceeded, then it does not returns an exception as in case of add() and addLast() function. Syntax: public boolean offerLast(E e) Parameters: This 2 min read BlockingDeque takeFirst() method in Java with Examples The takeFirst() method of BlockingDeque returns and removes the first element of the Deque container from it, waiting if necessary until an element becomes available.. The method throws an InterruptedException if it is interrupted while waiting. Syntax: public E takeFirst() Returns: This method retu 2 min read BlockingDeque pollFirst() method in Java with examples The pollFirst() method of BlockingDeque returns the front element in the Deque container, and deletes it. It returns null if the container is empty. Syntax: public E pollFirst() Parameters: This method does not accept any parameters. Returns: This method returns front element in the Deque container 2 min read BlockingDeque putFirst() method in Java with Examples The putFirst(E e) method of BlockingDeque inserts the specified element at the front of the queue represented by this deque. If the Deque is capacity restricted, then it will wait for the space to become available. Syntax: public void putFirst(E e) Parameters: This method accepts a mandatory paramet 2 min read BlockingQueue offer() method in Java with examples There are two types of offer() method for BlockingQueue interface:Note: The offer() method of BlockingQueue has been inherited from the Queue class in Java. offer(E e, long timeout, TimeUnit unit) The offer(E e, long timeout, TimeUnit unit) method of BlockingQueue inserts the element passed as param 6 min read BlockingDeque put() method in Java with Examples The put(E e) method of BlockingDeque inserts the specified element into the queue represented by this deque (in other words, at the tail of this deque). If the Deque is capacity restricted, then it will wait for the space to become available. Syntax: public void put(E e) Parameters: This method acce 2 min read Like