Java LinkedList push() Method
Example
Use a list as a stack, and add items to the top of 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);
}
}
Definition and Usage
The push() method adds an item to the beginning of the list.
It works like addFirst(). Together with pop(), 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 pop() method to take the first item out of the list.
Syntax
public void push(T item)
T refers to the data type of items in the list.
Parameter Values
| Parameter | Description |
|---|---|
| item | Required. The item to add to the beginning of the list. |
Technical Details
| Returns: | No return value. |
|---|
Related Pages
❮ LinkedList Methods