Yandex

Java Advanced ConceptsJava Advanced Concepts3

Java LinkedList peekLast() method
Syntax and Examples


Introduction

The peekLast() method in Java's LinkedList class allows you to retrieve the last element of the list without removing it. Think of it like glancing at the back of a queue - you see what's there, but it remains in place.

Syntax


public E peekLast()

Parameters

This method doesn’t accept any parameters.

Parameter Description

Return Value

The peekLast() method returns the last element in the list. If the list is empty, it returns null.

Examples

Example 1: Retrieving the Last Element

This example demonstrates how to use peekLast() to retrieve the last element of a LinkedList without removing it. We’ll create a list, add some elements, and then peek at the last one.


import java.util.LinkedList;

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

        String lastElement = myList.peekLast();

        System.out.println("Last element: " + lastElement);
        System.out.println("List after peekLast(): " + myList); // Verify list hasn't changed
    }
}


Last element: Cherry
List after peekLast(): [Apple, Banana, Cherry]

Explanation: We created a LinkedList named myList and added three string elements. Then, we used the peekLast() method to retrieve the last element ("Cherry"). Notice that after calling peekLast(), the list remains unchanged; "Cherry" is still at the end.

Example 2: Handling an Empty List

This example shows what happens when you try to use peekLast() on an empty LinkedList. It's important to handle this case because attempting to get the last element of an empty list would otherwise lead to errors.


import java.util.LinkedList;

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

        Integer lastElement = emptyList.peekLast();

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


The list is empty.

Explanation: We created an empty LinkedList called emptyList. Calling peekLast() on this empty list returns null. The code then checks if the returned value is null; if it is, a message indicating that the list is empty is printed.

Example 3: Using with other operations

This example showcases using peekLast() in conjunction with other LinkedList methods for common operations. We'll add elements and then retrieve last element to demonstrate its utility in a realistic scenario


import java.util.LinkedList;

public class PeekLastCombinedExample {
    public static void main(String[] args) {
        LinkedList<String> myData = new LinkedList<>();

        myData.add("Record 1");
        myData.add("Record 2");
        myData.add("Record 3");

        System.out.println("Initial List: " + myData);
        String last = myData.peekLast();
        if (last != null) {
            System.out.println("The Last record is: " + last );
        }
    }
}


Initial List: [Record 1, Record 2, Record 3]
The Last record is: Record 3

Explanation This example shows how the peekLast() method works along with other linked list methods. The LinkedList myData is initialized and then elements are added to it. Lastly we use peekLast() to get the last element without removing from the list.


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