Java Reference Library →

LinkedList poll() method - Syntax and Examples

LinkedList poll() method - Syntax and Examples

Learn about the Java LinkedList's `poll()` method. This tutorial covers its syntax, parameters, return values, and provides practical examples to illustrate how it removes and returns the head of a list.

This reference explains LinkedList poll() method - Syntax and Examples through its syntax, examples and boundaries. Use the linked course lessons when the surrounding Java concepts are unfamiliar.

  1. Use this reference

    Identify the documented behavior, read a complete example and check its inputs before applying it.

LinkedList poll() method - Syntax and Examples

Introduction

The poll() method in Java's LinkedList is a handy tool for removing and retrieving the first element of the list. Unlike its counterpart, remove(), poll() doesn’t throw an exception if the list is empty. Instead, it returns null, allowing you to handle such scenarios gracefully.

Syntax

public E poll()

Parameters

Parameter Description
N/A This method doesn't accept any parameters.

Return Value

The poll() method returns:

  • The first element of the list (the head) if the list is not empty.
  • null if the list is empty.

Examples

Example 1: Retrieving Head from a Non-Empty List

This example demonstrates how to use poll() when the LinkedList contains elements.

import java.util.LinkedList;

public class PollExample {
    public static void main(String[] args) {
        LinkedList<String> myList = new LinkedList<>(
                "apple", "banana", "cherry"
        );

        String head = myList.poll();

        System.out.println("Head element: " + head);
        System.out.println("List after poll(): " + myList);
    }
}
Head element: apple
List after poll(): [banana, cherry]

In this example, poll() removed “apple” from the beginning of the list and assigned it to the head variable. The remaining elements are then printed.

Example 2: Handling an Empty List

This demonstrates what happens when you call poll() on an empty LinkedList. It returns null, preventing a potential exception.

import java.util.LinkedList;

public class PollEmptyList {
    public static void main(String[] args) {
        LinkedList<Integer> emptyList = new LinkedList<>();

        Integer head = emptyList.poll();

        if (head == null) {
            System.out.println("The list is empty.");
        } else {
            System.out.println("Head element: " + head);
        }
    }
}
The list is empty.

Because emptyList was initially empty, poll() returned null. The conditional statement checks for this and prints the appropriate message.

Example 3: Using poll() in a Loop

This example shows how to continuously remove elements from the head of a LinkedList until it becomes empty, demonstrating a common use case.

import java.util.LinkedList;

public class PollLoopExample {
    public static void main(String[] args) {
        LinkedList<Double> numbers = new LinkedList<>(
                3.14, 2.71, 1.618, 0.577
        );

        while (numbers != null && !numbers.isEmpty()) {
            Double firstNumber = numbers.poll();
            System.out.println("Processing: " + firstNumber);
        }
    }
}
Processing: 3.14
Processing: 2.71
Processing: 1.618
Processing: 0.577

The loop continues as long as the list is not empty. Inside the loop, poll() removes and processes each element until the list becomes empty.

Using this reference

Check the example’s types, inputs and documented behavior before copying it into an application. Some APIs require external libraries, files or platform services; consult their setup notes.

Related explanations: Java Data Types - Primitive and Non-Primitive, Java Class - Blueprint of Objects, Exceptions in Java Introduction.

Supporting documentation: Official Java learning guide.

Reference checks are optional and do not count toward course progress.

    Reference library →