Java LinkedList pollLast() Method
Example
Take the last item out of a list:
import java.util.LinkedList;
public class Main {
public static void main(String[] args) {
LinkedList<String> cars = new LinkedList<String>();
cars.add("Volvo");
cars.add("BMW");
cars.add("Ford");
System.out.println(cars.pollLast());
System.out.println(cars);
}
}
Definition and Usage
The pollLast() method removes the last item from the list and returns it.
If the list is empty, it returns null. The removeLast() method does the same, but it throws an exception when the list is empty.
Tip: Use the pollFirst() method to take the first item out of the list.
Syntax
public T pollLast()
T refers to the data type of items in the list.
Parameter Values
None.
Technical Details
| Returns: | The last item in the list (the item that was removed), or null if the list is empty. |
|---|
Related Pages
❮ LinkedList Methods