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:
import java.util.concurrent.*;
class GFG {
public static void main(String[] args)
{
ConcurrentLinkedDeque<String> cld = new ConcurrentLinkedDeque<String>();
cld.add( "Welcome" );
cld.add( "To" );
cld.add( "Geeks" );
cld.add( "4" );
cld.add( "Geeks" );
System.out.println( "Elements in Deque: " + cld);
System.out.println( "The Last element is: " + cld.getLast());
}
}
|
Output:
Elements in Deque: [Welcome, To, Geeks, 4, Geeks]
The Last element is: Geeks
Program 2:
import java.util.concurrent.*;
class GFG {
public static void main(String[] args)
{
ConcurrentLinkedDeque<Integer> cld = new ConcurrentLinkedDeque<Integer>();
try {
System.out.println( "The Last element "
+ "is: " + cld.getLast());
}
catch (Exception e) {
System.out.println(e);
}
cld.add( 12 );
cld.add( 43 );
cld.add( 29 );
cld.add( 16 );
cld.add( 70 );
System.out.println( "Elements in "
+ "Deque: " + cld);
System.out.println( "The Last element is: " + cld.getLast());
}
}
|
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()
Last Updated :
17 Sep, 2018
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...