Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
communitycreator

What is the ConcurrentLinkedDeque.pollLast() 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.

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

The pollLast method retrieves and removes the last element of the ConcurrentLinkedDeque object.

Syntax

public E pollLast()

Function parameter(s)

This method doesn’t take any parameters.

Return value

This method retrieves and removes the last element of the Deque object. If the Deque object is empty, then the pollLast method returns null.

This method is similar to the removeLast method, except the removeLast method throws the NoSuchElementException if the deque is empty, whereas the pollLast method returns null.

Example

The example below demonstrates the use of the pollLast method. Click on the “Run” button to execute the given code.

import java.util.concurrent.ConcurrentLinkedDeque;
class PollLast {
public static void main( String args[] ) {
ConcurrentLinkedDeque<String> deque = new ConcurrentLinkedDeque<>();
deque.add("1"); // adding elements
deque.add("2");
deque.add("3");
System.out.println("The deque is " + deque);
// calling pollLast method
System.out.println("deque.pollLast() returns : " + deque.pollLast());
System.out.println("The deque is " + deque);
}
}

Explanation of the code

  • In line #1, we import the ConcurrentLinkedDeque class.

  • In line #4, we create a ConcurrentLinkedDeque object named deque.

  • From line #5 to 7, we use the deque object to add three elements ("1","2","3") to deque.

  • In line #10, we use the pollLast() method of the deque object to retrieve and remove the last element. In our case, 3 will be removed and returned.

RELATED TAGS

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.

Keep Exploring