The offerLast(E e) method of Deque Interface inserts the specified element into the end of the Deque if it is possible to do so immediately without violating capacity restrictions. This method is preferable to add() method since this method does not throws an exception when the capacity of the container is full since it returns false.
Syntax:
boolean offerLast(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 else it returns false.
Exceptions: The function throws three exceptions which are described as below:
- ClassCastException: when the class of the element to be entered prevents it from being added to this container.
- 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 offerLast() method of Deque:
Program 1: With the help of LinkedList.
import java.util.*;
public class GFG {
public static void main(String[] args)
throws IllegalStateException
{
Deque<Integer> DQ
= new LinkedList<Integer>();
if (DQ.offerLast( 10 ))
System.out.println( "The Deque is not full and 10 is inserted" );
else
System.out.println( "The Deque is full" );
if (DQ.offerLast( 15 ))
System.out.println( "The Deque is not full and 15 is inserted" );
else
System.out.println( "The Deque is full" );
if (DQ.offerLast( 25 ))
System.out.println( "The Deque is not full and 25 is inserted" );
else
System.out.println( "The Deque is full" );
if (DQ.offerLast( 20 ))
System.out.println( "The Deque is not full and 20 is inserted" );
else
System.out.println( "The Deque is full" );
System.out.println( "Deque: " + DQ);
}
}
|
Output:
The Deque is not full and 10 is inserted
The Deque is not full and 15 is inserted
The Deque is not full and 25 is inserted
The Deque is not full and 20 is inserted
Deque: [10, 15, 25, 20]
Program 2: With the help of ArrayDeque.
import java.util.*;
public class GFG {
public static void main(String[] args)
throws IllegalStateException
{
Deque<Integer> DQ
= new ArrayDeque<Integer>();
if (DQ.offerLast( 10 ))
System.out.println( "The Deque is not full and 10 is inserted" );
else
System.out.println( "The Deque is full" );
if (DQ.offerLast( 15 ))
System.out.println( "The Deque is not full and 15 is inserted" );
else
System.out.println( "The Deque is full" );
if (DQ.offerLast( 25 ))
System.out.println( "The Deque is not full and 25 is inserted" );
else
System.out.println( "The Deque is full" );
if (DQ.offerLast( 20 ))
System.out.println( "The Deque is not full and 20 is inserted" );
else
System.out.println( "The Deque is full" );
System.out.println( "Deque: " + DQ);
}
}
|
Output:
The Deque is not full and 10 is inserted
The Deque is not full and 15 is inserted
The Deque is not full and 25 is inserted
The Deque is not full and 20 is inserted
Deque: [10, 15, 25, 20]
Program 3: With the help of ConcurrentLinkedDeque.
import java.util.*;
import java.util.concurrent.ConcurrentLinkedDeque;
public class GFG {
public static void main(String[] args)
throws IllegalStateException
{
Deque<Integer> DQ
= new ConcurrentLinkedDeque<Integer>();
if (DQ.offerLast( 10 ))
System.out.println( "The Deque is not full and 10 is inserted" );
else
System.out.println( "The Deque is full" );
if (DQ.offerLast( 15 ))
System.out.println( "The Deque is not full and 15 is inserted" );
else
System.out.println( "The Deque is full" );
if (DQ.offerLast( 25 ))
System.out.println( "The Deque is not full and 25 is inserted" );
else
System.out.println( "The Deque is full" );
if (DQ.offerLast( 20 ))
System.out.println( "The Deque is not full and 20 is inserted" );
else
System.out.println( "The Deque is full" );
System.out.println( "Deque: " + DQ);
}
}
|
Output:
The Deque is not full and 10 is inserted
The Deque is not full and 15 is inserted
The Deque is not full and 25 is inserted
The Deque is not full and 20 is inserted
Deque: [10, 15, 25, 20]
Program 4: With the help of LinkedBlockingDeque.
import java.util.*;
import java.util.concurrent.LinkedBlockingDeque;
public class GFG {
public static void main(String[] args)
throws IllegalStateException
{
Deque<Integer> DQ
= new LinkedBlockingDeque<Integer>();
if (DQ.offerLast( 10 ))
System.out.println( "The Deque is not full and 10 is inserted" );
else
System.out.println( "The Deque is full" );
if (DQ.offerLast( 15 ))
System.out.println( "The Deque is not full and 15 is inserted" );
else
System.out.println( "The Deque is full" );
if (DQ.offerLast( 25 ))
System.out.println( "The Deque is not full and 25 is inserted" );
else
System.out.println( "The Deque is full" );
if (DQ.offerLast( 20 ))
System.out.println( "The Deque is not full and 20 is inserted" );
else
System.out.println( "The Deque is full" );
System.out.println( "Deque: " + DQ);
}
}
|
Output:
The Deque is not full and 10 is inserted
The Deque is not full and 15 is inserted
The Deque is not full and 25 is inserted
The Deque is not full and 20 is inserted
Deque: [10, 15, 25, 20]
Below programs illustrate exceptions thrown by this method:
Program 5: To show NullPointerException.
import java.util.*;
import java.util.concurrent.LinkedBlockingDeque;
public class GFG {
public static void main(String[] args)
throws NullPointerException
{
Deque<Integer> DQ
= new LinkedBlockingDeque<Integer>();
DQ.offerLast( 7855642 );
DQ.offerLast( 35658786 );
DQ.offerLast( 5278367 );
DQ.offerLast( null );
System.out.println( "Deque: " + DQ);
}
}
|
Output:
Exception in thread "main" java.lang.NullPointerException
at java.util.concurrent.LinkedBlockingDeque.offerLast(LinkedBlockingDeque.java:357)
at GFG.main(GFG.java:21)
Note: The other two exceptions are internal and are caused depending on the compiler hence it cannot be shown in the code.
Reference: https://docs.oracle.com/javase/8/docs/api/java/util/Deque.html#offerLast-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!
Last Updated :
21 Sep, 2018
Like Article
Save Article