Java LinkedList pollFirst() Method
Example
Take the first 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.pollFirst());
System.out.println(cars);
}
}
Definition and Usage
The pollFirst() method removes the first item from the list and returns it.
It works like poll(). If the list is empty, it returns null.
Tip: Use the pollLast() method to take the last item out of the list.
Syntax
public T pollFirst()
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), or null if the list is empty. |
|---|
Related Pages
❮ LinkedList Methods