The Wayback Machine - https://web.archive.org/web/20240618020226/https://www.geeksforgeeks.org/arraydeque-getlast-method-in-java/
Open In App

ArrayDeque getLast() Method in Java

Last Updated : 14 Oct, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

The java.util.ArrayDeque.getLast() method in Java is used to retrieve or fetch the last element of the ArrayDeque. In the process, the method does not delete the element from the deque instead it just returns the last element of the deque.
Syntax: 

Array_Deque.getLast()

Parameters: The method does not take any parameter.
Return Value: The method returns the last element present in the Deque. 
Exception: This method throws NoSuchElementException, if this deque is empty when called upon.

Below programs illustrate the Java.util.ArrayDeque.getLast() method:
 

Program 1: 
 

Java




// Java code to illustrate getLast() method of ArrayDeque
import java.util.*;
 
public class ArrayDequeDemo {
    public static void main(String args[])
    {
        // Creating an empty ArrayDeque
        ArrayDeque<String> de_que = new ArrayDeque<String>();
 
        // Use add() method to add elements into the Deque
        de_que.add("Welcome");
        de_que.add("To");
        de_que.add("Geeks");
        de_que.add("4");
        de_que.add("Geeks");
 
        // Displaying the ArrayDeque
        System.out.println("ArrayDeque: " + de_que);
 
        // Displaying the Last element
        System.out.println("The last element is: " + de_que.getLast());
       
        // clear the deque
        de_que.clear();
        try {
           de_que.getLast();
        } catch(Exception ex) {
           System.out.println("Exception  "+ ex);
         }
    }
}


Output: 

ArrayDeque: [Welcome, To, Geeks, 4, Geeks]
The last element is: Geeks


 

Program 2: Adding Integer elements into the Deque. 
 

Java




// Java code to illustrate getLast() method of ArrayDeque
import java.util.*;
 
public class ArrayDequeDemo {
    public static void main(String args[])
    {
        // Creating an empty ArrayDeque
        ArrayDeque<Integer> de_que = new ArrayDeque<Integer>();
 
        // Use add() method to add elements into the Deque
        de_que.add(10);
        de_que.add(15);
        de_que.add(30);
        de_que.add(20);
        de_que.add(5);
 
        // Displaying the ArrayDeque
        System.out.println("ArrayDeque: " + de_que);
 
        // Displaying the Last element
        System.out.println("The last element is: " + de_que.getLast());
    }
}


Output: 

ArrayDeque: [10, 15, 30, 20, 5]
The last element is: 5


 



Previous Article
Next Article

Similar Reads

Java.util.LinkedList.get(), getFirst(), getLast() in Java
In Java, the LinkedList class provides several methods for accessing the elements in the list. Here are the methods for getting the elements of a LinkedList: get(int index): This method returns the element at the specified position in the LinkedList. The index parameter is zero-based, so the first element has an index of 0.getFirst(): This method r
5 min read
LinkedBlockingDeque getLast() method in Java
The getLast() method of LinkedBlockingDeque returns the last 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 last element of this Deque container. Excepti
2 min read
LinkedList getLast() Method in Java
Linked List is a part of the Collection framework present in java.util package. This class is an implementation of the LinkedList data structure which is a linear data structure where the elements are not stored in contiguous locations and every element is a separate object with a data part and. The Java.util.LinkedList.getLast() method is used to
2 min read
ConcurrentLinkedDeque getLast() Method in Java
The java.util.concurrent.ConcurrentLinkedDeque.getLast() method is an in-built method in Java which returns the last element of the deque container. Syntax: Conn_Linked_Deque.getLast() Parameters: The method does not accept any parameter. Return Value: The method returns the last element present in the Deque. Exception: The function throws a NoSuch
2 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 tail of the Deque but does not delete it. Excepti
3 min read
Java 8 | ArrayDeque removeIf() method in Java with Examples
The removeIf() method of ArrayDeque is used to remove all those elements from ArrayDeque which satisfies a given predicate filter condition passed as a parameter to the method. This method returns true if some element are removed from the Vector. Java 8 has an important in-built functional interface which is Predicate. Predicate, or a condition che
3 min read
Java.util.ArrayDeque Class in Java | Set 1
java.util.ArrayDeque class describes an implementation of a resizable array structure which implements the Deque interface. Array deques has not immutable and can grow as necessary. They are not thread-safe and hence concurrent access by multiple threads is not supported unless explicitly synchronized. Null elements are invalid in this structure. M
5 min read
Java.util.ArrayDeque Class in Java | Set 2
Java.util.ArrayDeque Class in Java | Set 1 More Methods of util.ArrayDeque Class : 16. offer(Element e) : java.util.ArrayDeque.offer(Element e) : inserts element at the end of deque. Syntax : public boolean offer(Element e) Parameters : e - element to add Return : true, if element is added else; false 17. offerFirst(Element e) : java.util.ArrayDequ
5 min read
ArrayDeque offerLast() Method in Java
The Java.util.ArrayDeque.offerLast(Object element) method in Java is used to add a specific element at the end of this Deque. The function is similar to the addLast(), add() and offer() method of ArrayDeque in Java. Syntax: Array_Deque.offerLast(Object element) Parameters: The parameter element is of the type ArrayDeque and refers to the element to
2 min read
ArrayDeque toArray() Method in Java
The java.util.ArrayDeque.toArray() method is used to form an array of the same elements as that of the Deque. Basically, the method copies all the element from this deque to a new array. Syntax: Object[] arr = Array_Deque.toArray() Parameters: The method does not take any parameters. Return Value: The method returns an array containing the elements
2 min read