Java LinkedList pop() Method
Example
Use a list as a stack, and take the top item off the stack:
import java.util.LinkedList;
public class Main {
public static void main(String[] args) {
LinkedList<String> cars = new LinkedList<String>();
cars.push("Volvo");
cars.push("BMW");
cars.push("Ford");
System.out.println(cars.pop());
System.out.println(cars);
}
}
Definition and Usage
The pop() method removes the first item from the list and returns it.
It works like removeFirst(). Together with push(), it lets you use the list as a stack (a list where the last item added is the first item taken out).
Tip: Use the push() method to add an item to the beginning of the list.
Syntax
public T pop()
T refers to the data type of items in the list.
Parameter Values
None.
Technical Details
| Returns: | The first item in the list (the item that was removed). |
|---|---|
| Throws: | NoSuchElementException - if the list is empty. |
Related Pages
❮ LinkedList Methods