Yandex

Java Advanced ConceptsJava Advanced Concepts3

Java 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.


Welcome to ProgramGuru

Sign up to start your journey with us

Support ProgramGuru.org

You can support this website with a contribution of your choice.

When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.

PayPal

UPI

PhonePe QR

MALLIKARJUNA M