The getFirst() method of LinkedBlockingDeque returns the front most element in the Deque container. If the LinkedBlockingDeque is empty, then on function call it returns a NoSuchElementException.
Syntax:
public E getLast()
Parameters: This method does not accept any parameters.
Returns: This method returns the first element or the head of this Deque container.
Exception: The function throws only one exception i.e., NoSuchElementException when the deque is empty
Below programs illustrate getFirst() method of LinkedBlockingDeque:
Program 1:
// Java Program Demonstrate getFirst() // 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>(); // Add numbers to front of LinkedBlockingDeque LBD.addFirst(7855642); LBD.addFirst(35658786); LBD.addFirst(5278367); LBD.addFirst(74381793); // before removing print queue System.out.println("Linked Blocking Deque: " + LBD); // prints the first element System.out.println("Linked Blocking Deque first element: " + LBD.getFirst()); } } |
Linked Blocking Deque: [74381793, 5278367, 35658786, 7855642] Linked Blocking Deque first element: 74381793
Program 2:
// Java Program Demonstrate getFirst() // 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>(); // Add numbers to front of LinkedBlockingDeque LBD.addFirst(7855642); LBD.addFirst(35658786); LBD.addFirst(5278367); LBD.addFirst(74381793) LBD.clear(); // Since the container is empty it will throw exception System.out.println("Linked Blocking Deque first element: " + LBD.getFirst()); } } |
Output:
Exception in thread "main" java.util.NoSuchElementException
at java.util.concurrent.LinkedBlockingDeque.getFirst(LinkedBlockingDeque.java:553)
at GFG.main(GFG.java:28)
Reference: https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/LinkedBlockingDeque.html#getFirst()
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:
- Java.util.LinkedList.get(), getFirst(), getLast() in Java
- LinkedList getFirst() Method in Java
- ArrayDeque getFirst() Method in Java
- ConcurrentLinkedDeque getFirst() method in Java
- Deque getFirst() method in Java
- LinkedBlockingDeque putFirst() method in Java
- LinkedBlockingDeque iterator() method in Java
- LinkedBlockingDeque contains() method in Java
- LinkedBlockingDeque getLast() method in Java
- LinkedBlockingDeque removeLastOccurrence() method in Java
- LinkedBlockingDeque size() method in Java
- LinkedBlockingDeque clear() method in Java
- LinkedBlockingDeque addLast() method in Java
- LinkedBlockingDeque add() method in Java
- LinkedBlockingDeque addFirst() method in Java
- LinkedBlockingDeque push() method in Java
- LinkedBlockingDeque pop() method in Java
- LinkedBlockingDeque offer() method in Java
- LinkedBlockingDeque offerLast() method in Java
- LinkedBlockingDeque peek() 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.

