LinkedBlockingDeque offerFirst() method in Java
The offerFirst(E e) method of LinkedBlockingDeque 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 offer(E e)
Parameters: This method accepts a mandatory parameter e which is the element to be inserted at the front of the LinkedBlockingDeque.
Returns: This method returns true if the element has been inserted, else it returns false.
Below programs illustrate offerFirst() method of LinkedBlockingDeque:
Program 1:
// Java Program Demonstrate offerFirst() // method of LinkedBlockingDeque import java.util.concurrent.LinkedBlockingDeque; import java.util.*; public class GFG { public static void main(String[] args) throws IllegalStateException { // create object of LinkedBlockingDeque LinkedBlockingDeque<Integer> LBD = new LinkedBlockingDeque<Integer>(4); // Add numbers to end of LinkedBlockingDeque LBD.offerFirst(7855642); LBD.offerFirst(35658786); LBD.offerFirst(5278367); LBD.offerFirst(74381793); // Cannot be inserted LBD.offerFirst(10); // cannot be inserted hence returns false if (!LBD.offerFirst(10)) System.out.println("The element 10 cannot be inserted"+ " as capacity is full"); // before removing print queue System.out.println("Linked Blocking Deque: " + LBD); } } |
The element 10 cannot be inserted as capacity is full Linked Blocking Deque: [74381793, 5278367, 35658786, 7855642]
Program 2:
// Java Program Demonstrate offerFirst() // method of LinkedBlockingDeque import java.util.concurrent.LinkedBlockingDeque; import java.util.*; public class GFG { public static void main(String[] args) throws IllegalStateException { // create object of LinkedBlockingDeque LinkedBlockingDeque<String> LBD = new LinkedBlockingDeque<String>(4); // Add numbers to end of LinkedBlockingDeque LBD.offerFirst("abc"); LBD.offerFirst("gopu"); LBD.offerFirst("geeks"); LBD.offerFirst("richik"); // Cannot be inserted LBD.offerFirst("hii"); // cannot be inserted hence returns false if (!LBD.offerFirst("hii")) System.out.println("The element 'hii' cannot be" +" inserted as capacity is full"); // before removing print queue System.out.println("Linked Blocking Deque: " + LBD); } } |
The element 'hii' cannot be inserted as capacity is full Linked Blocking Deque: [richik, geeks, gopu, abc]
Recommended Posts:
- ConcurrentLinkedDeque offerFirst() method in Java
- Deque offerFirst() method in Java
- ArrayDeque offerFirst() Method in Java
- BlockingDeque offerFirst() method in Java with Examples
- LinkedBlockingDeque put() method in Java
- LinkedBlockingDeque take() method in Java
- LinkedBlockingDeque pop() method in Java
- LinkedBlockingDeque contains() method in Java
- LinkedBlockingDeque add() method in Java
- LinkedBlockingDeque iterator() method in Java
- LinkedBlockingDeque peekFirst() method in Java
- LinkedBlockingDeque takeLast() method in Java
- LinkedBlockingDeque drainTo() method in Java with Example
- LinkedBlockingDeque takeFirst() method in Java
- LinkedBlockingDeque toArray() method in Java with Example
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.



