VOOZH about

URL: https://www.geeksforgeeks.org/java/deque-getfirst-method-in-java/

⇱ Deque getFirst() method in Java - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Deque getFirst() method in Java

Last Updated : 11 Jul, 2025

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


Output: 
Deque: [7855642, 35658786, 5278367, 74381793]
Deque's head: 7855642

 

Program 2: With the help of ArrayDeque


Output: 
Deque: [7855642, 35658786, 5278367, 74381793]
Deque's head: 7855642

 

Program 3: With the help of LinkedBlockingDeque


Output: 
Deque: [7855642, 35658786, 5278367, 74381793]
Deque's head: 7855642

 

Program 4: With the help of ConcurrentLinkedDeque


Output: 
Deque: [7855642, 35658786, 5278367, 74381793]
Deque's head: 7855642

 

Program 2: 

Output: 

Exception in thread "main" java.util.NoSuchElementException
 at java.util.LinkedList.getFirst(LinkedList.java:244)
 at GFG.main(GFG.java:29)
Comment