In this shot, we will discuss the Deque.offerLast
method in Java, which is present in the Deque
interface inside the java.util
package.
Deque.offerLast
is used to add or insert the given element in the last of the deque. The Deque.offerLast
method does throw an exception if the adding element violates the restriction of the deque’s size, in which case it returns false
.
Deque.offerLast(element);
The Deque.offerLast
method takes an element that needs to be added in the end of the deque.
The Deque.offerLast
method returns a Boolean variable:
True
: If the element is added in the deque.False
: If the element is not added in the deque.Let’s take a look at the code snippet.
import java.util.*;class Solution3 {public static void main(String[] args) {Deque<Integer> dq= new ArrayDeque<>();dq.add(10);dq.add(20);dq.add(30);dq.add(40);dq.add(50);System.out.println("the elements of deque are:-\n"+dq);int x=60;dq.offerLast(x);System.out.println("deque after offerLast operation is:-\n"+ dq);}}
java.util.*
package to include the Deque
interface.Integer
type and added elements 10, 20, 30, 40, and 50.x
of int type that is to be added at the end of the deque.x
in front of deque using deque.offerLast()
method.offerLast()
method.In this way, we can use the Deque.offerLast
method in Java.