YouTip LogoYouTip

Java Linkedlist Getfirst

# Java LinkedList getFirst() Method [![Image 3: Java LinkedList](#) Java LinkedList](#) * * * `getFirst()` is a method provided by the `LinkedList` class in Java, used to retrieve the first element in the linked list. This method belongs to the `java.util.LinkedList` class and is one of the fundamental 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 ### Function Description The `getFirst()` method is used to retrieve but not remove the first element (head element) of the linked list. This method has the same functionality as the `peekFirst()` method, but their behaviors differ 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 Examples ### Basic Usage ## Example import java.util.LinkedList; public class LinkedListExample { public static void main(String[] args){ // Create a LinkedList LinkedList 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 numbers = new LinkedList(); // Using getFirst() - throws NoSuchElementException if empty try { int first = numbers.getFirst(); System.out.println("First: " + first); } catch (java.util.NoSuchElementException e) { System.out.println("List is empty"); } // Using peekFirst() - returns null if empty Integer peekFirst = numbers.peekFirst(); System.out.println("Peek first: " + peekFirst);// Output: Peek first: null numbers.add(10); numbers.add(20); System.out.println("After adding: " + numbers.getFirst());// Output: After adding: 10 ## Output List is empty Peek first: null After adding: 10 ### Key Differences | Method | Empty List Behavior | Return Type | |--------|---------------------|-------------| | `getFirst()` | Throws `NoSuchElementException` | `E` (primitive) | | `peekFirst()` | Returns `null` | `E` (object, can be null) | ## Practical Applications 1. **Queue Operations**: In queue implementations, `getFirst()` is often used to get the next element to process. 2. **Stack Operations**: When using LinkedList as a stack, `getFirst()` retrieves the top element. 3. **Data Processing**: Useful when you need to access the first element without removing it. ## Notes - The method does not remove the element from the list - If the list is empty, it throws `NoSuchElementException` - For safe access without exceptions, use `peekFirst()` instead - This method is equivalent to `get(0)` but more semantically clear for linked lists ## Related Methods - `peekFirst()` - Retrieves but does not remove the first element, returns null if empty - `removeFirst()` - Retrieves and removes the first element - `addFirst(E e)` - Adds an element to the beginning of the list - `get(int index)` - Gets element at specified index
← Java Linkedlist IndexofJava Linkedlist Contains β†’