Yandex

Java Advanced ConceptsJava Advanced Concepts3

Java LinkedList pollLast() method
Syntax and Examples


Introduction

The pollLast() method in Java's LinkedList class is a handy tool for removing and retrieving the last element of the list. It’s similar to getLast(), but unlike that method which throws an exception when the list is empty, pollLast() gracefully returns null.

Syntax


public E pollLast()

Description

The pollLast() method removes and returns the last element of this list. It provides a non-blocking alternative to getLast(), which throws a NoSuchElementException if the list is empty. If this list is empty, then it returns null.

Parameters

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

Return Value

The pollLast() method returns the last element of this list. If this list is empty, it returns null.

Examples

Example 1: Retrieving and Removing the Last Element

This example demonstrates how to use pollLast() to get and remove the last element from a LinkedList. We'll create a list, add some elements, then retrieve and remove the final one using pollLast().


import java.util.LinkedList;

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

        String lastElement = myList.pollLast();

        System.out.println("Removed element: " + lastElement);
        System.out.println("Remaining list: " + myList);
    }
}


Removed element: Cherry
Remaining list: [Apple, Banana]

Explanation: We create a LinkedList containing three fruits. Then pollLast() removes and returns "Cherry", which is the last element. The remaining list contains only "Apple" and "Banana".

Example 2: Handling an Empty List

This example shows what happens when you try to use pollLast() on an empty LinkedList. Instead of throwing an error, it returns null.


import java.util.LinkedList;

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

        Integer lastElement = emptyList.pollLast();

        System.out.println("Removed element: " + lastElement);
        System.out.println("Remaining list: " + emptyList);
    }
}


Removed element: null
Remaining list: []

Explanation: The LinkedList is initialized as an empty list. Calling pollLast() returns null because there are no elements to remove. The list remains empty.

Example 3: Using pollLast() in a Loop

This example demonstrates using pollLast() within a loop to process elements from the end of a LinkedList until it's empty.


import java.util.LinkedList;

public class PollLastLoop {
    public static void main(String[] args) {
        LinkedList<Double> numbers = new LinkedList<>();
        numbers.add(1.0);
        numbers.add(2.0);
        numbers.add(3.0);

        System.out.println("Processing elements from the end:");
        while (numbers.size() > 0) {
            Double lastNumber = numbers.pollLast();
            System.out.println("Processed: " + lastNumber);
        }
    }
}


Processing elements from the end:
Processed: 3.0
Processed: 2.0
Processed: 1.0

Explanation: The code creates a LinkedList with three double values and then iterates through the list using a while loop. In each iteration, pollLast() removes and prints the last element of the list 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