Yandex

Java Advanced ConceptsJava Advanced Concepts3

Java LinkedList peekFirst() method
Syntax and Examples


Introduction

The peekFirst() method in Java's LinkedList class allows you to examine the first element of the list without removing it. Think of it like taking a quick peek at what's on top of a stack – you see it, but it stays put.

Syntax

    
public E peekFirst()

Parameters

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

Return Value

The peekFirst() method returns the first element of the list. If the list is empty, it returns null.

Examples

Example 1: Retrieving the First Element

This example demonstrates how to use peekFirst() to get the first element of a linked list without modifying the list itself. It also shows what happens when the list is empty.

    
import java.util.LinkedList;

public class PeekFirstExample {

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

        String firstElement = myList.peekFirst();

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

        // Verify that the original list remains unchanged
        System.out.println("Original list: " + myList);
    }
}


The first element is: Apple
Original list: [Apple, Banana, Cherry]

Explanation: We created a LinkedList containing strings. We then used peekFirst() to retrieve the first element, "Apple". The output confirms that the original list remains intact after using peekFirst().

Example 2: Handling an Empty List

This example shows how to handle the case where the linked list is empty when calling peekFirst(). This prevents a potential NullPointerException.

    
import java.util.LinkedList;

public class EmptyListPeekExample {

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

        Integer firstElement = emptyList.peekFirst();

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


The list is empty.

Explanation: We created an empty LinkedList. When we called peekFirst() on this empty list, it returned null, which was then handled correctly by the if-else statement. This demonstrates a robust way to use the method and avoid potential errors.

Example 3: Using peekFirst in a Loop

This example shows how you might use peekFirst() in a loop, although it's not its primary intended purpose. It highlights that peekFirst() doesn't modify the list.

        
import java.util.LinkedList;

public class PeekFirstLoopExample {

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

        // Simulating a loop using peekFirst (not the most efficient approach)
        for (int i = 0; i < numbers.size(); i++) {
            Double first = numbers.peekFirst();
            System.out.println("First element: " + first);
            numbers.removeFirst(); // Removing for demonstration purposes, not a typical use case with peekFirst.
        }
    }
}

First element: 1.0
First element: 2.5
First element: 3.7

Explanation: This example demonstrates how to use peekFirst in a loop, although it's not its primary purpose. The removeFirst() is added here solely for the sake of demonstration and would typically *not* be used with peekFirst().


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