1. Introduction

In Java, LinkedList is a widely used class for storing a list of elements in a doubly-linked list format. Iterating through a LinkedList can be done using various approaches such as using an Iterator, ListIterator, enhanced for loop, or Java 8 forEach with lambda expressions. In this post, we will explore different ways to iterate through a LinkedList in Java.

2. Program Steps

1. Create and initialize a LinkedList.

2. Iterate through the LinkedList using different methods and print each element.

3. Code Program

import java.util.Iterator;
import java.util.LinkedList;
import java.util.ListIterator;

public class LinkedListIterationExample {

    public static void main(String[] args) {

        // Step 1: Create and initialize a LinkedList
        LinkedList<String> animals = new LinkedList<>();
        animals.add("Dog");
        animals.add("Cat");
        animals.add("Horse");
        System.out.println("Original LinkedList: " + animals);

        // Iterating using enhanced for loop
        System.out.println("Using enhanced for loop:");
        for (String animal : animals) {
            System.out.println(animal);
        }

        // Iterating using Iterator
        System.out.println("Using Iterator:");
        Iterator<String> iterator = animals.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }

        // Iterating using ListIterator
        System.out.println("Using ListIterator:");
        ListIterator<String> listIterator = animals.listIterator();
        while (listIterator.hasNext()) {
            System.out.println(listIterator.next());
        }

        // Iterating using forEach and lambda expression (Java 8 and above)
        System.out.println("Using forEach and lambda expression:");
        animals.forEach(animal -> System.out.println(animal));
    }
}

Output:

Original LinkedList: [Dog, Cat, Horse]
Using enhanced for loop:
Dog
Cat
Horse
Using Iterator:
Dog
Cat
Horse
Using ListIterator:
Dog
Cat
Horse
Using forEach and lambda expression:
Dog
Cat
Horse

4. Step By Step Explanation

Step 1: We create and initialize a LinkedList named animals with the elements "Dog", "Cat", and "Horse".

After Step 1, we use different methods to iterate through the LinkedList and print each element:

– Enhanced for loop: We use an enhanced for loop which is a concise way to iterate over all elements of the LinkedList.

– Iterator: We obtain an Iterator for the LinkedList and use the hasNext and next methods to traverse the list.

– ListIterator: Similarly, we obtain a ListIterator for the LinkedList and use the hasNext and next methods to traverse the list.

– forEach and lambda expression (Java 8 and above): We use the forEach method with a lambda expression to print each element of the LinkedList.

In each method of iteration, the elements of the LinkedList are printed in the order they are inserted, demonstrating different approaches to iterate through a LinkedList in Java.