LinkedBlockingDeque getFirst() method in Java Last Updated : 14 Sep, 2018 Comments Improve Suggest changes Like Article Like Report 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 // 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()); } } Output: Linked Blocking Deque: [74381793, 5278367, 35658786, 7855642] Linked Blocking Deque first element: 74381793 Program 2: Java // 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() Comment More infoAdvertise with us Next Article LinkedBlockingDeque clear() method in Java gopaldave Follow Improve Article Tags : Misc Java Java-Collections Java - util package Java-Functions Java-LinkedBlockingDeque +2 More Practice Tags : JavaJava-CollectionsMisc Similar Reads LinkedBlockingDeque add() method in Java The add(E e) method of LinkedBlockingDeque inserts the element passed in the parameter to the end of the Deque is there is space. If the LinkedBlockingDeque is capacity restricted and no space is left for insertion, it returns an IllegalStateException. It works exactly in the same way as addLast() m 2 min read LinkedBlockingDeque in Java with Examples The LinkedBlockingDeque class in Java is a part of the Java Collection Framework. It was introduced in JDK 1.6 and it belongs to java.util.concurrent package. It is a Deque(Doubly Ended Queue) which blocks a thread if that thread tries to take elements out of it while the Deque is empty. It implemen 14 min read LinkedBlockingDeque iterator() method in Java The iterator() method of LinkedBlockingDeque returns an iterator over the elements in this deque in a proper sequence. The elements will be returned in order from first (head) to last (tail). The returned iterator is a "weakly consistent" iterator. Syntax: public Iterator iterator() Parameters: This 2 min read LinkedBlockingDeque peekLast() method in Java The peekLast() method of LinkedBlockingDeque returns the last element in the Deque container, but does not deletes it. It returns null if the container is empty. Syntax: public E peekLast() Parameters: This method does not accept any parameters. Returns: This method returns last element in the Deque 2 min read LinkedBlockingDeque offer() method in Java The offer(E e) method of LinkedBlockingDeque 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 acc 2 min read LinkedBlockingDeque element() method in Java The element() method of LinkedBlockingDeque returns the element at the front the container. It does not deletes the element in the container. This method returns the head of the queue represented by this deque. Syntax: public void element() Parameters: This method does not accept any parameter. Retu 2 min read LinkedBlockingDeque addFirst() method in Java The addFirst(E e) method of LinkedBlockingDeque inserts the element passed in the parameter to the front of the Deque if there is space. If the LinkedBlockingDeque is capacity restricted and no space is left for insertion, it returns an IllegalStateException. Syntax: public void addFirst(E e) Parame 2 min read LinkedBlockingDeque pop() method in Java The pop() method of LinkedBlockingDeque pops an element from the stack represented by this deque. In other words, it removes and returns the first element of this deque. It returns null if the container is empty. Syntax: public E pop() Parameters: This method does not accept any parameters. Returns: 2 min read LinkedBlockingDeque getFirst() method in Java 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 2 min read LinkedBlockingDeque clear() method in Java The clear() method of LinkedBlockingDeque erases all the elements that are present in the LinkedBlockingDeque container. The container becomes empty after the function is called. Syntax: public void clear() Parameters: This method does not accepts any parameters Returns: This method does not returns 2 min read Like