Java ArrayDeque Class: pollLast() Method
public E pollLast()
The pollLast() method is used to retrieve and remove the last element of a given deque, or returns null if this deque is empty.
Package: java.util
Java Platform: Java SE 8
Syntax:
pollLast()
Return Value:
the tail of this deque, or null if this deque is empty
Return Value Type: E - the type of elements held in this collection
Pictorial Presentation
Example: Java ArrayDeque Class: pollLast() Method
import java.util.ArrayDeque;
import java.util.Deque;
public class Main {
public static void main(String[] args) {
// Create an array deque
Deque<Integer> deque = new ArrayDeque<Integer>(8);
// use add() method to add elements in the deque
deque.add(100);
deque.add(200);
deque.add(150);
deque.add(95);
// Print all the elements of the original deque
System.out.println("Elements of the original deque:");
for (Integer number : deque) {
System.out.println("Number = " + number);
}
int retval = deque.pollLast();
System.out.println("Removed element: " + retval);
// printing all the elements available in deque after using pollLast()
for (Integer number : deque) {
System.out.println("Number = " + number);
}
}
}
Output:
Elements of the original deque: Number = 100 Number = 200 Number = 150 Number = 95 Removed element: 95 Number = 100 Number = 200 Number = 150
Java Code Editor:
Previous:pollFirst Method
Next:pop Method
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/java-tutorial/util/arraydeque/java_arraydeque_polllast.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics