Deque getFirst() method in Java Last Updated : 19 Sep, 2023 Comments Improve Suggest changes Like Article Like Report The getFirst() method of Deque Interface returns the first element or the head of the Deque. It does not deletes the element. It throws an exception when the Deque is empty. Syntax: E getFirst() Parameters: This method does not accepts any parameter.Returns: This method returns the first element or the head of the Deque but does not delete it.Exception: The function throws NoSuchElementException when the Deque is empty and the function is called. Below programs illustrate getFirst() method of Deque:Program 1: With the help of LinkedList. Java // Java Program Demonstrate getFirst() // method of Deque import java.util.*; public class GFG { public static void main(String[] args) throws IllegalStateException { // create object of Deque Deque<Integer> DQ = new LinkedList<Integer>(); // Add numbers to end of Deque DQ.add(7855642); DQ.add(35658786); DQ.add(5278367); DQ.add(74381793); // print Deque System.out.println("Deque: " + DQ); // print head System.out.println("Deque's head: " + DQ.getFirst()); } } Output: Deque: [7855642, 35658786, 5278367, 74381793] Deque's head: 7855642 Program 2: With the help of ArrayDeque. Java // Java Program Demonstrate getFirst() // method of Deque import java.util.*; public class GFG { public static void main(String[] args) throws IllegalStateException { // create object of Deque Deque<Integer> DQ = new ArrayDeque<Integer>(); // Add numbers to end of Deque DQ.add(7855642); DQ.add(35658786); DQ.add(5278367); DQ.add(74381793); // print Deque System.out.println("Deque: " + DQ); // print head System.out.println("Deque's head: " + DQ.getFirst()); } } Output: Deque: [7855642, 35658786, 5278367, 74381793] Deque's head: 7855642 Program 3: With the help of LinkedBlockingDeque. Java // Java Program Demonstrate getFirst() // method of Deque import java.util.*; import java.util.concurrent.LinkedBlockingDeque; public class GFG { public static void main(String[] args) throws IllegalStateException { // create object of Deque Deque<Integer> DQ = new LinkedBlockingDeque<Integer>(); // Add numbers to end of Deque DQ.add(7855642); DQ.add(35658786); DQ.add(5278367); DQ.add(74381793); // print Deque System.out.println("Deque: " + DQ); // print head System.out.println("Deque's head: " + DQ.getFirst()); } } Output: Deque: [7855642, 35658786, 5278367, 74381793] Deque's head: 7855642 Program 4: With the help of ConcurrentLinkedDeque. Java // Java Program Demonstrate getFirst() // method of Deque import java.util.*; import java.util.concurrent.ConcurrentLinkedDeque; public class GFG { public static void main(String[] args) throws IllegalStateException { // create object of Deque Deque<Integer> DQ = new ConcurrentLinkedDeque<Integer>(); // Add numbers to end of Deque DQ.add(7855642); DQ.add(35658786); DQ.add(5278367); DQ.add(74381793); // print Deque System.out.println("Deque: " + DQ); // print head System.out.println("Deque's head: " + DQ.getFirst()); } } Output: Deque: [7855642, 35658786, 5278367, 74381793] Deque's head: 7855642 Program 2: Java // Java Program Demonstrate getFirst() // method of Deque when it is empty import java.util.*; public class GFG { public static void main(String[] args) throws IllegalStateException { // create object of Deque Deque<Integer> DQ = new LinkedList<Integer>(); // Add numbers to end of Deque DQ.add(7855642); DQ.add(35658786); DQ.add(5278367); DQ.add(74381793); // print Deque System.out.println("Deque: " + DQ); // print head System.out.println("Deque's head: " + DQ.getFirst()); DQ.clear(); // Deque is empty now hence exception System.out.println("Deque's head: " + DQ.getFirst()); } } Output: Exception in thread "main" java.util.NoSuchElementException at java.util.LinkedList.getFirst(LinkedList.java:244) at GFG.main(GFG.java:29) Comment More infoAdvertise with us Next Article Deque getLast() method in Java G gopaldave Follow Improve Article Tags : Java Java - util package Java-Functions java-deque Practice Tags : Java Similar Reads Deque Interface in Java Deque Interface present in java.util package is a subtype of the queue interface. The Deque is related to the double-ended queue that supports adding or removing elements from either end of the data structure. It can either be used as a queue(first-in-first-out/FIFO) or as a stack(last-in-first-out/ 9 min read Deque add() method in Java The add(E e) method of Deque Interface inserts the element passed in the parameter to the end of the Deque if there is space. If the Deque is capacity restricted and no space is left for insertion, it returns an IllegalStateException. The function returns true on successful insertion. Syntax:  bool 4 min read Deque addFirst() method in Java with Examples The addFirst(E e) method of Deque Interface inserts the element passed in the parameter to the front of the Deque if there is space. If the Deque is capacity restricted and no space is left for insertion, it returns an IllegalStateException. The function returns true on successful insertion. Syntax: 4 min read Deque addLast() method in Java The addLast(E e) method of Deque Interface inserts the element passed in the parameter to the end of the Deque if there is space. If the Deque is capacity restricted and no space is left for insertion, it returns an IllegalStateException. The function returns true on successful insertion. Syntax: bo 4 min read Deque contains() method in Java The contains(E e) method of Deque Interface check for the presence of the element e in the Deque container. If the Deque contains one occurrence of the element, then it returns true else it returns false. Syntax: boolean contains(Object o) Parameters: This method accepts a mandatory parameter o whic 5 min read Deque descendingIterator() Method in Java The descendingIterator() method of the Deque Interface returns an iterator over the elements in this deque in reverse order. The elements will be returned from the last to the first order. This can be used with any class implementing the Deque Interface like LinkedList, ArrayDeque, LinkedBlockingDeq 2 min read Deque element() method in Java The element() method of Deque Interface returns the element at the front the container. It does not deletes the element in the container. This method returns the head of the Deque. The method throws an exception when the Deque is empty. Syntax:  E element() Parameters: This method does not accepts 3 min read Deque getFirst() method in Java The getFirst() method of Deque Interface returns the first element or the head of the Deque. It does not deletes the element. It throws an exception when the Deque is empty. Syntax:  E getFirst() Parameters: This method does not accepts any parameter.Returns: This method returns the first element o 3 min read Deque getLast() method in Java The getLast() method of Deque Interface returns the last element or the tail of the Deque. It does not deletes the element. It throws an exception when the Deque is empty. Syntax: E getLast() Parameters: This method does not accepts any parameter. Returns: This method returns the last element or the 3 min read Deque iterator() method in Java The iterator() method of Deque Interface 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: Iterator iterator() Parameters: This method doe 3 min read Like