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 NoSuchElementException when the deque is empty.

Below programs illustrate the ConcurrentLinkedDeque.getLast() method:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

/* Java Program to Demonstrate getLast()
   method of ConcurrentLinkedDeque */
  
import java.util.concurrent.*;
class GFG {
    public static void main(String[] args)
    {
  
        // Creating an empty Deque
        ConcurrentLinkedDeque<String> cld = new ConcurrentLinkedDeque<String>();
  
        // Add elements into the Deque
        cld.add("Welcome");
        cld.add("To");
        cld.add("Geeks");
        cld.add("4");
        cld.add("Geeks");
  
        // Displaying the Deque
        System.out.println("Elements in Deque: " + cld);
  
        // Displaying the Last element
        System.out.println("The Last element is: " + cld.getLast());
    }
}

chevron_right


Output:

Elements in Deque: [Welcome, To, Geeks, 4, Geeks]
The Last element is: Geeks

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

/* Java Program to Demonstrate getLast()
   method of ConcurrentLinkedDeque */
  
import java.util.concurrent.*;
class GFG {
    public static void main(String[] args)
    {
  
        // Creating an empty Deque
        ConcurrentLinkedDeque<Integer> cld = new ConcurrentLinkedDeque<Integer>();
  
        try {
            // Displaying the Last element
            System.out.println("The Last element "
                               + "is: " + cld.getLast());
        }
        catch (Exception e) {
            // Displaying the Exception
            System.out.println(e);
        }
  
        // Add elements into the Deque
        cld.add(12);
        cld.add(43);
        cld.add(29);
        cld.add(16);
        cld.add(70);
  
        // Displaying the Deque
        System.out.println("Elements in "
                           + "Deque: " + cld);
  
        // Displaying the Last element
        System.out.println("The Last element is: " + cld.getLast());
    }
}

chevron_right


Output:

java.util.NoSuchElementException
Elements in Deque: [12, 43, 29, 16, 70]
The Last element is: 70

Reference:https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentLinkedDeque.html#getLast()



My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.