ConcurrentLinkedDeque offerLast() Method in Java
Last Updated :
26 Nov, 2018
The java.util.concurrent.ConcurrentLinkedDeque.offerLast() method is an inbuilt method in Java which inserts the specified element, passed as a parameter, to the end of the deque.
Syntax:
Conn_Linked_Deque.offerLast(Object elem)
Parameters: The method accepts a parameter elem which species the element to be inserted to the end of the deque.
Return Value: The function returns True if the element is successfully added into the deque and returns False otherwise.
Exception: The function throws a NullPointerException if the passed parameter is NULL.
Below programs illustrate the ConcurrentLinkedDeque.offerLast() 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());
if (cld.offerLast( "GFG" )) {
System.out.println( "The Inserted element is: " +
cld.getLast());
}
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
The Inserted element is: GFG
Elements in Deque: [Welcome, To, Geeks, 4, Geeks, GFG]
The Last element is: GFG
Program 2:
import java.util.concurrent.*;
class GFG {
public static void main(String[] args)
{
ConcurrentLinkedDeque<Integer> cld =
new ConcurrentLinkedDeque<Integer>();
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());
try {
cld.offerLast( null );
}
catch (Exception e) {
System.out.println(e);
}
if (cld.offerLast( 24 )) {
System.out.println( "The Inserted element is: "
+ cld.getLast());
}
System.out.println( "Elements in Deque: "
+ cld);
System.out.println( "The Last element is: " +
cld.getLast());
}
}
|
Output:
Elements in Deque: [12, 43, 29, 16, 70]
The Last element is: 70
java.lang.NullPointerException
The Inserted element is: 24
Elements in Deque: [12, 43, 29, 16, 70, 24]
The Last element is: 24
Reference: https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ConcurrentLinkedDeque.html#offerLast()
Share your thoughts in the comments
Please Login to comment...