Yandex

Java Advanced ConceptsJava Advanced Concepts3

Java LinkedList pollFirst() method
Syntax and Examples


Introduction

The pollFirst() method in Java's LinkedList class is a powerful tool for managing the order of elements. It retrieves and removes the first element from the linked list. If the list is empty, it returns null instead of throwing an exception (unlike removeFirst()).

Syntax

public E pollFirst()

Parameters

Parameter Description
None This method doesn't accept any parameters.

Return Value

Return Value Description
E The first element in this list. Returns null if this list is empty.

Examples

Example 1: Retrieving and Removing the First Element

This example demonstrates how to use pollFirst() to remove the first element from a linked list. We create a LinkedList, add some elements, then retrieve and remove the first one using pollFirst().

import java.util.LinkedList;

public class PollFirstExample1 {
    public static void main(String[] args) {
        LinkedList<String> list = new LinkedList<>();
        list.add("Apple");
        list.add("Banana");
        list.add("Cherry");

        System.out.println("Original List: " + list);

        String firstElement = list.pollFirst();

        System.out.println("Removed First Element: " + firstElement);
        System.out.println("List after removing first element: " + list);
    }
}
Original List: [Apple, Banana, Cherry]
Removed First Element: Apple
List after removing first element: [Banana, Cherry]

In this example, pollFirst() removed "Apple" from the beginning of the list and returned it. The remaining elements are now in the list.

Example 2: Handling an Empty List

This example showcases what happens when you call pollFirst() on an empty linked list. It demonstrates that instead of throwing an exception, pollFirst() simply returns null.

import java.util.LinkedList;

public class PollFirstExample2 {
    public static void main(String[] args) {
        LinkedList<Integer> list = new LinkedList<>(); // Empty List

        System.out.println("Original List: " + list);

        Integer firstElement = list.pollFirst();

        System.out.println("Removed First Element: " + firstElement);  // Will print null
        System.out.println("List after attempting to remove first element: " + list); // Remains empty.
    }
}
Original List: []
Removed First Element: null
List after attempting to remove first element: []

Since the list was initially empty, pollFirst() returned null. The list remains unchanged.

Example 3: Using with a Loop

This example shows how to use pollFirst() in a loop to process elements from the front of the LinkedList until it's empty.

import java.util.LinkedList;

public class PollFirstExample3 {
    public static void main(String[] args) {
        LinkedList<Double> list = new LinkedList<>();
        list.add(1.0);
        list.add(2.5);
        list.add(3.7);

        System.out.println("Original List: " + list);

        while (list != null && !list.isEmpty()) {
            Double element = list.pollFirst();
            System.out.println("Processing: " + element);
        }

        System.out.println("List after processing: " + list); // Should be empty.
    }
}
Original List: [1.0, 2.5, 3.7]
Processing: 1.0
Processing: 2.5
Processing: 3.7
List after processing: []

The loop continues as long as the list is not empty and contains elements to process. Each iteration retrieves and removes the first element, prints it, and then repeats 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