The ConcurrentLinkedDeque.removeLast() is an in-built function in Java which removes the last element in the deque. The function throws a NoSuchElementException if this deque is empty.
Syntax:
Conn_Linked_Deque.removeLast()
Parameters: The function does not accepts any parameter.
Return Values: The function returns the last element in the deque.
Exception: The function throws a NoSuchElementException if this deque is empty.
Below program illustrates the removeLast() method:
Program 1: This program involves deque with Integer elements.
// Java Program Demonstrate removeLast() // method of ConcurrentLinkedDeque import java.util.concurrent.*; class ConcurrentLinkedDequeDemo { public static void main(String[] args) { ConcurrentLinkedDeque<Integer> cld = new ConcurrentLinkedDeque<Integer>(); cld.addFirst(12); cld.addFirst(70); cld.addFirst(1009); cld.addFirst(475); // Displaying the existing LinkedDeque System.out.println("Elements in" + "the LinkedDeque: " + cld); // Display the last element System.out.println("Element removed : " + cld.peekLast()); // Remove the Last element cld.removeLast(); // Displaying the elements System.out.println("Elements in" + "the LinkedDeque: " + cld); }} |
Elements inthe LinkedDeque: [475, 1009, 70, 12] Element removed : 12 Elements inthe LinkedDeque: [475, 1009, 70]
Program 2: This program involves deque with String elements.
// Java Program Demonstrate removeLast() // method of ConcurrentLinkedDeque import java.util.concurrent.*; class ConcurrentLinkedDequeDemo { public static void main(String[] args) { ConcurrentLinkedDeque<String> cld = new ConcurrentLinkedDeque<String>(); cld.addFirst("GFG"); cld.addFirst("Gfg"); cld.addFirst("GeeksforGeeks"); cld.addFirst("Geeks"); // Displaying the existing LinkedDeque System.out.println("Elements in" + "the LinkedDeque: " + cld); // Display the last element System.out.println("Element removed : " + cld.peekLast()); // Remove the Last element cld.removeLast(); // Displaying the elements System.out.println("Elements in" + "the LinkedDeque: " + cld); }} |
Elements inthe LinkedDeque: [Geeks, GeeksforGeeks, Gfg, GFG] Element removed : GFG Elements inthe LinkedDeque: [Geeks, GeeksforGeeks, Gfg]
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.


