Java Program to Access elements from a LinkedList

Updated on December 12, 2024
Access elements from a linkedlist header image

Introduction

LinkedList in Java is a part of the Java Collections Framework and it is used extensively when performance in insertion or deletion operations is critical. It provides a doubly linked list implementation of the List and Deque interfaces, allowing for efficient positional access and alteration of elements.

In this article, you will learn how to access elements from a Java LinkedList. Discover the various methods provided by the LinkedList class to retrieve or check elements. Each section of this article guides you through different examples and explains the results.

Accessing Elements in LinkedList

Retrieve the First and Last Element

  1. Create an instance of a LinkedList.

  2. Add some elements to the list.

  3. Use the getFirst() and getLast() methods to retrieve the first and last elements respectively.

    java
    LinkedList<String> list = new LinkedList<>();
    list.add("Apple");
    list.add("Banana");
    list.add("Cherry");
    
    String firstElement = list.getFirst();
    String lastElement = list.getLast();
    System.out.println("First Element: " + firstElement);
    System.out.println("Last Element: " + lastElement);
    

    The output of the code would be:

    First Element: Apple
    Last Element: Cherry

    getFirst() fetches the first element ('Apple') and getLast() fetches the last element ('Cherry') from the LinkedList.

Accessing Elements by Index

  1. Understand that LinkedList allows accessing elements by their index, similar to an array or an ArrayList.

  2. Use the get(int index) method to retrieve an element at a specific position in the LinkedList.

    java
    String elementAtIndexTwo = list.get(2);
    System.out.println("Element at Index 2: " + elementAtIndexTwo);
    

    This code fetches the element at index 2, which is 'Cherry'.

Check if an Element Exists

  1. Sometimes, it's necessary to check whether a LinkedList contains a particular item before proceeding.

  2. Use the contains(Object o) method to check if an element is present in the list.

    java
    boolean containsBanana = list.contains("Banana");
    System.out.println("Contains Banana: " + containsBanana);
    

    If 'Banana' is an element in the list, the method returns true.

Iterating Through LinkedList

  1. To access all elements in a LinkedList, iterating through the list is commonly used.

  2. Utilize for loops or enhanced for loops to iterate over elements.

    java
    for (String item : list) {
        System.out.println(item);
    }
    

    This loop prints all elements: 'Apple', 'Banana', and 'Cherry'.

Using Iterators

  1. LinkedList implementations support various iterator types including basic, descending, and ListIterator.

  2. Use iterators to traverse the LinkedList in different orders.

    java
    Iterator<String> iterator = list.iterator();
    while (iterator.hasNext()) {
        System.out.println("Item via Iterator: " + iterator.next());
    }
    
    Iterator<String> descendingIterator = list.descendingIterator();
    while (descendingIterator.hasNext()) {
        System.out.println("Item via Descending Iterator: " + descendingIterator.next());
    }
    

    The first snippet uses a regular iterator to access elements in the order they were added, while the second uses a descending iterator to access elements in reverse order.

Conclusion

Effectively accessing elements from a LinkedList in Java includes using methods like getFirst(), getLast(), and get(index), or by employing iterators for more complex traversal needs. Whether accessing a single element or iterating through the LinkedList, Java provides multiple methods to suit different requirements. Through the examples provided, embrace these techniques to enhance manipulation and retrieval operations in LinkedLists, ensuring efficient and effective usage in your Java applications.