Open In App

Deque getFirst() method in Java

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
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)


Last Updated : 19 Sep, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads