Deque addLast() method in Java
The addLast(E e) method of Deque Interface inserts the element passed in the parameter to the end of the Deque if there is space. If the Deque is capacity restricted and no space is left for insertion, it returns an IllegalStateException. The function returns true on successful insertion.
Syntax:
boolean addLast(E e)
Parameters: This method accepts a mandatory parameter e which is the element to be inserted in the end of the Deque.
Returns: This method returns true on successful insertion.
Exceptions: The function throws four exceptions which are described as below:
- ClassCastException: when the class of the element to be entered prevents it from being added to this container.
- IllegalStateException: when the capacity of the container is full and insertion is done.
- IllegalArgumentException: when some property of the element prevents it to be added to the Deque.
- NullPointerException: when the element to be inserted is passed as null and the Deque’s interface does not allow null elements.
Below programs illustrate implementation of addLast() method by various classes that implement Deque Interface:
Program 1: With the help of LinkedList.
// Java Program Demonstrate addLast() // method of Deque import java.util.*; public class GFG { public static void main(String[] args) throws IllegalStateException { // create object of De1ue Deque<Integer> DQ = new LinkedList<Integer>(); // Add numbers to end of Deque DQ.addLast(7855642); DQ.addLast(35658786); DQ.addLast(5278367); DQ.addLast(74381793); // before removing print Deque System.out.println("Deque: " + DQ); } } |
Deque: [7855642, 35658786, 5278367, 74381793]
Program 2: With the help of LinkedBlockingDeque.
// Java Program Demonstrate addLast() // method of Deque import java.util.*; import java.util.concurrent.LinkedBlockingDeque; public class GFG { public static void main(String[] args) throws IllegalStateException { // create object of De1ue Deque<Integer> DQ = new LinkedBlockingDeque<Integer>(); // Add numbers to end of Deque DQ.addLast(7855642); DQ.addLast(35658786); DQ.addLast(5278367); DQ.addLast(74381793); // before removing print Deque System.out.println("Deque: " + DQ); } } |
Deque: [7855642, 35658786, 5278367, 74381793]
Program 3: With the help of ArrayDeque.
// Java Program Demonstrate addLast() // method of Deque import java.util.*; public class GFG { public static void main(String[] args) throws IllegalStateException { // create object of De1ue Deque<Integer> DQ = new ArrayDeque<Integer>(); // Add numbers to end of Deque DQ.addLast(7855642); DQ.addLast(35658786); DQ.addLast(5278367); DQ.addLast(74381793); // before removing print Deque System.out.println("Deque: " + DQ); } } |
Deque: [7855642, 35658786, 5278367, 74381793]
Program 4: With the help of ConcurrentLinkedDeque.
// Java Program Demonstrate addLast() // method of Deque import java.util.*; import java.util.concurrent.ConcurrentLinkedDeque; public class GFG { public static void main(String[] args) throws IllegalStateException { // create object of De1ue Deque<Integer> DQ = new ConcurrentLinkedDeque<Integer>(); // Add numbers to end of Deque DQ.addLast(7855642); DQ.addLast(35658786); DQ.addLast(5278367); DQ.addLast(74381793); // before removing print Deque System.out.println("Deque: " + DQ); } } |
Deque: [7855642, 35658786, 5278367, 74381793]
Below programs illustrate exceptions thrown by addLast() method:
Program 5: To show NullPointerException.
// Java Program Demonstrate addLast() // method of DeQue when Null is passed import java.util.*; import java.util.concurrent.LinkedBlockingDeque; public class GFG { public static void main(String[] args) throws IllegalStateException { // create object of DeQue Deque<Integer> DQ = new LinkedBlockingDeque<Integer>(); // Add numbers to end of DeQue DQ.addLast(7855642); DQ.addLast(35658786); DQ.addLast(5278367); try { // when null is inserted DQ.addLast(null); } catch (Exception e) { System.out.println("Exception thrown " + "while inserting null: " + e); } } } |
Exception thrown while inserting null: java.lang.NullPointerException
Program 6: To show IllegalStateException.
// Java Program Demonstrate addLast() // method of Deque when capacity is full import java.util.*; import java.util.concurrent.LinkedBlockingDeque; public class GFG { public static void main(String[] args) throws IllegalStateException { // create object of Deque Deque<Integer> DQ = new LinkedBlockingDeque<Integer>(3); // Add numbers to end of Deque DQ.addLast(7855642); DQ.addLast(35658786); DQ.addLast(5278367); try { // when capacity is full DQ.addLast(10); } catch (Exception e) { System.out.println("Exception thrown " + "while inserting null: " + e); } } } |
Exception thrown while inserting null: java.lang.IllegalStateException: Deque full
Note: The other two exceptions are internal and are caused depending on the compiler hence cannot be shown in the online compiler.
Reference: https://docs.oracle.com/javase/8/docs/api/java/util/Deque.html#addLast-E-
Recommended Posts:
- LinkedList addLast() Method in Java
- ArrayDeque addLast() Method in Java
- ConcurrentLinkedDeque addLast() method in Java
- LinkedBlockingDeque addLast() method in Java
- BlockingDeque addLast() method in Java with Examples
- Deque add() method in Java
- Deque contains() method in Java
- Deque getFirst() method in Java
- Deque descendingIterator() method in Java
- Deque getLast() method in Java
- Deque offerFirst() method in Java
- Deque element() method in Java
- Deque offerLast() method in Java
- Deque offer() method in Java
- Deque iterator() method in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



