The addLast(E e) method of LinkedBlockingDeque inserts the element passed in the parameter to the end of the Deque if there is space. If the LinkedBlockingDeque is capacity restricted and no space is left for insertion, it returns an IllegalStateException.
Syntax:
public void addLast(E e)
Parameters: This method accepts a mandatory parameter e which is the element to be inserted in the end of the LinkedBlockingDeque.
Returns: This method does not returns anything.
Exception:
- IllegalStateException: if the element cannot be added at this time due to capacity restrictions
- NullPointerException: if the specified element is null
Below programs illustrate addLast() 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.addLast( 7855642 );
LBD.addLast( 35658786 );
LBD.addLast( 5278367 );
LBD.addLast( 74381793 );
System.out.println( "Linked Blocking Deque: " + LBD);
}
}
|
Output:
Linked Blocking Deque: [7855642, 35658786, 5278367, 74381793]
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>( 3 );
LBD.addLast( 7855642 );
LBD.addLast( 35658786 );
LBD.addLast( 5278367 );
LBD.addLast( 74381793 );
System.out.println( "Linked Blocking Deque: " + LBD);
}
}
|
Output:
Exception in thread "main" java.lang.IllegalStateException: Deque full
at java.util.concurrent.LinkedBlockingDeque.addLast(LinkedBlockingDeque.java:335)
at GFG.main(GFG.java:23)
Program 3:
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.addLast( 7855642 );
LBD.addLast( 35658786 );
LBD.addLast( 5278367 );
LBD.addLast( null );
System.out.println( "Linked Blocking Deque: " + LBD);
}
}
|
Output:
Exception in thread "main" java.lang.NullPointerException
at java.util.concurrent.LinkedBlockingDeque.offerLast(LinkedBlockingDeque.java:357)
at java.util.concurrent.LinkedBlockingDeque.addLast(LinkedBlockingDeque.java:334)
at GFG.main(GFG.java:23)
Reference: https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/LinkedBlockingDeque.html#addLast(E)
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!