Java LinkedList getFirst() Method

Java LinkedList Java LinkedList


getFirst()is a method in JavaLinkedListprovided by the class, used to get the first element of a linked list. This method belongs to thejava.util.LinkedListclass, and is one of the basic methods for operating on linked list data structures.

Method Declaration

public E getFirst()

Return Value

Returns the first element (head element) of the linked list


Method Details

Description

getFirst()The method is used to retrieve but not remove the first element (head element) of the linked list. This method is functionally the same as thepeekFirst()method, but behaves differently when the linked list is empty.

Time Complexity

O(1) - Because LinkedList internally maintains a reference to the head node, it can be accessed directly

Usage Example

Basic Usage

Example

import java.util.LinkedList;

public class LinkedListExample {
    public static void main(String[] args) {
        // Create a LinkedList
        LinkedList<String> fruits = new LinkedList<>();
       
        // Add elements
        fruits.add("Apple");
        fruits.add("Banana");
        fruits.add("Cherry");
       
        // Use getFirst() to get the first element
        String firstFruit = fruits.getFirst();
        System.out.println("The first fruit is: " + firstFruit);  // Output: The first fruit is: Apple
    }
}

Comparison with Similar Methods

Example

LinkedList<Integer> numbers = new LinkedList<>();
numbers.add(10);
numbers.add(20);

// getFirst() and peekFirst() behave the same in a non-empty linked list
System.out.println(numbers.getFirst());  // Output: 10
System.out.println(numbers.peekFirst()); // Output: 10

// But they behave differently when the linked list is empty
numbers.clear();

// System.out.println(numbers.getFirst()); // Throws NoSuchElementException
System.out.println(numbers.peekFirst());    // Output: null

Notes

Exception Handling

When the linked list is empty, calling thegetFirst()method will throwNoSuchElementException. Therefore, it is best to check whether the linked list is empty before using it:

Example

if (!myList.isEmpty()) {
    E element = myList.getFirst();
    // Process the element
} else {
    System.out.println("The linked list is empty");
}

Alternative Methods

If you want to returnnullinstead of throwing an exception, you can use thepeekFirst()method:

Example

E element = myList.peekFirst();  // Returns null when the linked list is empty

Practical Application Scenarios

Scenario 1: Processing a Queue

Example

LinkedList<Order> orderQueue = new LinkedList<>();
// Add orders...

// Get and process the first order in the queue
if (!orderQueue.isEmpty()) {
    Order firstOrder = orderQueue.getFirst();
    processOrder(firstOrder);
}

Scenario 2: Implementing Stack Operations

Although LinkedList is usually used as a queue, it can also be used to implement a stack (last-in, first-out):

Example

LinkedList<String> stack = new LinkedList<>();
stack.push("First");  // Equivalent to addFirst()
stack.push("Second");
stack.push("Third");

// View the top element of the stack without removing it
String top = stack.getFirst();  // Returns "Third"

Summary

getFirst()method is aLinkedListsimple but practical method in the class, allowing us to quickly access the first element of a linked list. Remember its difference frompeekFirst()and choose which method to use based on actual needs. In cases where it may be empty, perform a null check before use or consider usingpeekFirst()to avoid runtime exceptions.

Java LinkedList Java LinkedList

Other Extensions