Skip to content
Related Articles

Related Articles

Deque getLast() method in Java
  • Last Updated : 20 Nov, 2020
GeeksforGeeks - Summer Carnival Banner

The getLast() method of Deque Interface returns the last element or the tail of the Deque. It does not delete the element. It throws an exception when the Deque is empty. 

Syntax:  

E getLast()

Parameters: This method does not accept any parameter.

Returns: Thie method returns the last element or the tail 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 getLast() method of Deque:

Program 1: With the help of LinkedList.  

Java




// Java Program Demonstrate getLast()
// 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.getLast());
    }
}
Output: 
Deque: [7855642, 35658786, 5278367, 74381793]
Deque's head: 74381793



 

Program 2: With the help of ArrayDeque

Java




// Java Program Demonstrate getLast()
// 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.getLast());
    }
}
Output: 
Deque: [7855642, 35658786, 5278367, 74381793]
Deque's head: 74381793



 

Program 3: With the help of ConcurrentLinkedDeque

Java




// Java Program Demonstrate getLast()
// 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.getLast());
    }
}
Output: 
Deque: [7855642, 35658786, 5278367, 74381793]
Deque's head: 74381793



 

Program 4: With the help of LinkedBlockingDeque

Java




// Java Program Demonstrate getLast()
// 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.getLast());
    }
}
Output: 
Deque: [7855642, 35658786, 5278367, 74381793]
Deque's head: 74381793



 

Program 5: 

Java




// Java Program Demonstrate getLast()
// 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.getLast());
 
        DQ.clear();
 
        // Deque is empty now hence exception
        System.out.println("Deque's head: " + DQ.getLast());
    }
}

Output: 

Exception in thread "main" java.util.NoSuchElementException
    at java.util.LinkedList.getLast(LinkedList.java:257)
    at GFG.main(GFG.java:29)

Reference: https://docs.oracle.com/javase/8/docs/api/java/util/Deque.html#getLast–
 

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :