The offer(E e) method of BlockingDeque 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 to be inserted in the end of the BlockingDeque.
Returns: This method returns true if the element has been inserted, else it returns false.
Note: The offer() method of BlockingDeque has been inherited from the LinkedBlockingDeque class in Java.
Below programs illustrate offer() method of BlockingDeque:
Program 1:
// Java Program Demonstrate offer() // 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.offer(7855642); BD.offer(35658786); BD.offer(5278367); BD.offer(74381793); // Cannot be inserted BD.offer(10); // cannot be inserted hence returns false if (!BD.offer(10)) System.out.println("The element 10 cannot be inserted" + " as capacity is full"); // before removing print queue System.out.println("Blocking Deque: " + BD); } } |
The element 10 cannot be inserted as capacity is full Blocking Deque: [7855642, 35658786, 5278367, 74381793]
Program 2:
// Java Program Demonstrate offer() // 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.offer("abc"); BD.offer("gopu"); BD.offer("geeks"); BD.offer("richik"); // Cannot be inserted BD.offer("hii"); // cannot be inserted hence returns false if (!BD.offer("hii")) System.out.println("The element 'hii' cannot be inserted" + " as capacity is full"); // before removing print queue System.out.println("Linked Blocking Deque: " + BD); } } |
The element 'hii' cannot be inserted as capacity is full Blocking Deque: [abc, gopu, geeks, richik]
Reference: https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/BlockingDeque.html#offer(E)
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- BlockingDeque putLast() function in Java with Examples
- BlockingDeque takeLast() method in Java with Examples
- BlockingDeque removeFirstOccurrence() method in Java with Examples
- BlockingDeque push() method in Java with examples
- BlockingDeque pollLast() method in Java with examples
- BlockingDeque pollFirst() method in Java with examples
- BlockingDeque poll() method in Java with examples
- BlockingDeque peek() method in Java with examples
- BlockingDeque offerLast() method in Java with examples
- BlockingDeque element() method in java with examples
- BlockingDeque iterator() method in Java with examples
- BlockingDeque offerFirst() method in Java with Examples
- BlockingDeque add() method in Java with Examples
- BlockingDeque addFirst() method in Java with Examples
- BlockingDeque addLast() method in Java with Examples
- BlockingDeque contains() method in Java with Examples
- BlockingDeque put() method in Java with Examples
- BlockingDeque putFirst() method in Java with Examples
- BlockingDeque remove() method in Java with Examples
- BlockingDeque removeLastOccurrence() method in Java with Examples
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.

