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.
Exception: The function throws only one exception i.e., NoSuchElementException when the deque is empty
Below programs illustrate getLast() method of LinkedBlockingDeque:
Program 1:
import java.util.concurrent.LinkedBlockingDeque;
import java.util.*;
public class GFG {
public static void main(String[] args)
throws IllegalStateException
{
LinkedBlockingDeque<Integer> LBD
= new LinkedBlockingDeque<Integer>();
LBD.addFirst( 7855642 );
LBD.addFirst( 35658786 );
LBD.addFirst( 5278367 );
LBD.addFirst( 74381793 );
System.out.println( "Linked Blocking Deque: " + LBD);
System.out.println( "Linked Blocking Deque first element: " +
LBD.getLast());
}
}
|
Output:
Linked Blocking Deque: [74381793, 5278367, 35658786, 7855642]
Linked Blocking Deque first element: 7855642
Program 2:
import java.util.concurrent.LinkedBlockingDeque;
import java.util.*;
public class GFG {
public static void main(String[] args)
throws IllegalStateException
{
LinkedBlockingDeque<Integer> LBD
= new LinkedBlockingDeque<Integer>();
LBD.addFirst( 7855642 );
LBD.addFirst( 35658786 );
LBD.addFirst( 5278367 );
LBD.addFirst( 74381793 )
LBD.clear();
System.out.println( "Linked Blocking Deque first element: " +
LBD.getLast());
}
}
|
Output:
Exception in thread "main" java.util.NoSuchElementException
at java.util.concurrent.LinkedBlockingDeque.getLast(LinkedBlockingDeque.java:553)
at GFG.main(GFG.java:28)
Reference: https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/LinkedBlockingDeque.html#getLast()
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
14 Sep, 2018
Like Article
Save Article