ConcurrentLinkedDeque offerFirst() method in Java
The java.util.concurrent.ConcurrentLinkedDeque.offerFirst() method is an inbuilt method in Java which inserts the specified element, passed as a parameter, in the front of the deque.
Syntax:
Conn_Linked_Deque.offerFirst(Object elem)
Parameters: The method accepts a parameter elem which species the element to be inserted to the front of the deque.
Return Value: The function returns True if the element is successfully added into the deque and returns False otherwise.
Exception: The function throws a NullPointerException if the passed parameter is NULL.
Below programs illustrate the ConcurrentLinkedDeque.offerFirst() method:
Program 1:
/* Java Program to Demonstrate offerFirst() method of ConcurrentLinkedDeque */ import java.util.concurrent.*; class GFG { public static void main(String[] args) { // Creating an empty Deque ConcurrentLinkedDeque<String> cld = new ConcurrentLinkedDeque<String>(); // Add elements into the Deque cld.add("Welcome"); cld.add("To"); cld.add("Geeks"); cld.add("4"); cld.add("Geeks"); // Displaying the Deque System.out.println("Elements in Deque: " + cld); // Displaying the First element System.out.println("The First element is: " + cld.getFirst()); /* Insert an element at the front of the deque */ if (cld.offerFirst("GFG")) { // Displaying the First element System.out.println("The Inserted element is: " + cld.getFirst()); } // Displaying the Deque System.out.println("Elements in Deque: " + cld); // Displaying the First element System.out.println("The First element is: " + cld.getFirst()); } } |
Elements in Deque: [Welcome, To, Geeks, 4, Geeks] The First element is: Welcome The Inserted element is: GFG Elements in Deque: [GFG, Welcome, To, Geeks, 4, Geeks] The First element is: GFG
Program 2:
/* Java Program to Demonstrate offerFirst() method of ConcurrentLinkedDeque */ import java.util.concurrent.*; class GFG { public static void main(String[] args) { // Creating an empty Deque ConcurrentLinkedDeque<Integer> cld = new ConcurrentLinkedDeque<Integer>(); // Add elements into the Deque cld.add(12); cld.add(43); cld.add(29); cld.add(16); cld.add(70); // Displaying the Deque System.out.println("Elements in Deque: " + cld); // Displaying the First element System.out.println("The First element is: " + cld.getFirst()); try { cld.offerFirst(null); } catch (Exception e) { System.out.println(e); } /* Insert an element at the front of the deque */ if (cld.offerFirst(74)) { // Displaying the First element System.out.println("The Inserted element is: " + cld.getFirst()); } // Displaying the Deque System.out.println("Elements in Deque: " + cld); // Displaying the First element System.out.println("The First element is: " + cld.getFirst()); } } |
Elements in Deque: [12, 43, 29, 16, 70] The First element is: 12 java.lang.NullPointerException The Inserted element is: 74 Elements in Deque: [74, 12, 43, 29, 16, 70] The First element is: 74
Recommended Posts:
- Deque offerFirst() method in Java
- LinkedBlockingDeque offerFirst() method in Java
- ArrayDeque offerFirst() Method in Java
- BlockingDeque offerFirst() method in Java with Examples
- ConcurrentLinkedDeque add() method in Java
- ConcurrentLinkedDeque addLast() method in Java
- ConcurrentLinkedDeque toArray() method in Java with Example
- ConcurrentLinkedDeque getFirst() method in Java
- ConcurrentLinkedDeque addFirst() method in Java
- ConcurrentLinkedDeque offerLast() Method in Java
- ConcurrentLinkedDeque getLast() Method in Java
- ConcurrentLinkedDeque descendingIterator() method in Java with Example
- ConcurrentLinkedDeque peekLast() method in Java
- ConcurrentLinkedDeque removeFirst() method in Java
- ConcurrentLinkedDeque clear() 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.



