Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

offerlast
java
communitycreator

What is the ConcurrentLinkedDeque.offerLast() method in Java?

Programming Bytes

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Answers Code

The ConcurrentLinkedDeque is thread-safe unbounded deque. The null value is not permitted as an element. We can use the ConcurrentLinkedDeque when multiple threads share a single deque.

The offerLast method can be used to add an element as the tail(end/last) element of the ConcurrentLinkedDeque object.

Syntax

public boolean offerLast(E e)

Parameters

The offerLast method takes the element to the deque as a parameter. This method throws NullPointerException if the argument is null.

Return value

The offerLast method returns true if the element is added to the deque.

Code

The code below demonstrates how to use the offerLast method:

import java.util.concurrent.ConcurrentLinkedDeque;
class OfferLast {
public static void main( String args[] ) {
ConcurrentLinkedDeque<String> deque = new ConcurrentLinkedDeque<>();
deque.offerLast("1");
System.out.println("The deque is " + deque);
deque.offerLast("2");
System.out.println("The deque is " + deque);
}
}

Explanation

In the above code:

  • In line 1, we import the ConcurrentLinkedDeque class.

  • In line 4, we create a ConcurrentLinkedDeque object with the name deque.

  • In line 5, we use the offerLast method of the deque object to add an element ("1") to the end of the deque. Then, we print it.

  • In line 8, we use the offerLast method to add an element ("2") to the end of the deque. Finally, we print it. Now the deque is [1,2].

RELATED TAGS

offerlast
java
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Answers Code
Keep Exploring