Traverse a linked list java

LinkedList doesn’t support the random access, to find an element we have to traverses the LinkedList. Let’s see how we will traverse a linked list java. There are some methods that can be useful to iterate the LinkedList. We have different types of iterator for linked list in java.

Traverse a linked list java

Here is the table content of the article will we will cover this topic.
1. iterator() method
2. spliterator() method
3. listiterator() method
4. java arraylist foreach() method

1. iterator() method

The iterator() method is declared in the Iterable interface, It is implemented by AbstractSequentialList class. In LinkedList, it returns an object of the iterator that contains all elements of the LinkedList. The iterator iterates the elements of LinkedList in sequential order. To get the element we have to use the next() method of Iterator. Lets traverse a linked list java

public Iterator<E> iterator()

This method is declared in Iterable interface but it is overridden in the AbstractSequentialList class.

Let’s see in java linkedlist iterator and see how to iterate Linkedlist in java.

import java.util.Iterator;
import java.util.LinkedList;
public class ExampleOfLinkedList 
{
	public static void main(String[] args) 
    {
		LinkedList<Integer> listOfNumbers = new LinkedList<Integer>();
		listOfNumbers.add(1);
		listOfNumbers.add(2);
		listOfNumbers.add(3);
		listOfNumbers.add(4);
		listOfNumbers.add(5);
		listOfNumbers.add(6);
		listOfNumbers.add(7);
		listOfNumbers.add(8);
		
		Iterator iterator = listOfNumbers.iterator();
		System.out.println("Element traversed by iterator: ");
		while(iterator.hasNext())
			System.out.println(iterator.next());
    }
}

Output: Element traversed by iterator:
1
2
3
4
5
6
7
8

2. spliterator() method

The spliterator() method returns a late-binding and fail-fast Spliterator. It returns a Spliterator of the same elements as the LinkedList. The spliterator object contains all the elements those present in ArrayList.

import java.util.LinkedList;
import java.util.Spliterator;
public class ExampleOfLinkedList
{
	public static void main(String[] args) 
    {
		LinkedList<Integer> listOfNumbers = new LinkedList<Integer>();
		listOfNumbers.add(1);
		listOfNumbers.add(2);
		listOfNumbers.add(3);
		listOfNumbers.add(4);
		listOfNumbers.add(5);
		listOfNumbers.add(6);
		listOfNumbers.add(7);
		listOfNumbers.add(8);
		
		Spliterator<Integer> iterator = listOfNumbers.spliterator();
		System.out.println("Element traversed by iterator: ");
			iterator.forEachRemaining(ele -> System.out.println(ele));
    }
}

Output: Element traversed by iterator:
1
2
3
4
5
6
7
8

3. listiterator() method

The listIterator() method returns an object of listIterator. It can only used of List type of collection. It contains all elements of the LinkedList and returns an object of listIterator of the same elements as the LinkedList. It is also used to iterate the elements sequentially. The useful point about listIterator, it can iterate the element in both directions, it means we can move forward or backward. The listIterator() method is overloaded in ArrayList:

 public ListIterator<E> listIterator()

Return, this method returns a list iterator over the elements in the ArrayList (in proper sequence).

public ListIterator listIterator(int index)

import java.util.LinkedList;
import java.util.ListIterator;
public class ExampleOfLinkedList 
{
	public static void main(String[] args) 
    {
		LinkedList<Integer> listOfNumbers = new LinkedList<Integer>();
		listOfNumbers.add(1);
		listOfNumbers.add(2);
		listOfNumbers.add(3);
		listOfNumbers.add(4);
		listOfNumbers.add(5);
		listOfNumbers.add(6);
		listOfNumbers.add(7);
		listOfNumbers.add(8);
		
		ListIterator<Integer> iterator1 = listOfNumbers.listIterator();
		System.out.println("ListIterator in forward direction: ");
		while(iterator1.hasNext())
			System.out.println(iterator1.next());
		
		System.out.println("ListIterator in backward direction: ");
		while(iterator1.hasPrevious())
			System.out.println(iterator1.previous());
		
		ListIterator<Integer> iterator2 = listOfNumbers.listIterator(1);
		System.out.println("ListIterator in forward direction: ");
		while(iterator2.hasNext())
			System.out.println(iterator2.next());
		
		System.out.println("ListIterator in backward direction: ");
		while(iterator2.hasPrevious())
			System.out.println(iterator2.previous());
    }
}

Output: ListIterator in forward direction:
1
2
3
4
5
6
7
8
ListIterator in backward direction:
8
7
6
5
4
3
2
1
ListIterator in forward direction:
2
3
4
5
6
7
8
ListIterator in backward direction:
8
7
6
5
4
3
2
1

foreach(Consumer c) method

The forEach(Consumer action) method is used to iterate the LinkedList. It takes one parameter of Consumer type. You must read the Consumer interface in Java after that you can understand it easily.

public void forEach(Consumer<? super E> action)
import java.util.LinkedList;

public class ExampleOfLinkedList
{
	public static void main(String[] args) 
    {
		LinkedList<Integer> listOfNumbers = new LinkedList<Integer>();
		listOfNumbers.add(1);
		listOfNumbers.add(2);
		listOfNumbers.add(3);
		listOfNumbers.add(4);
		listOfNumbers.add(5);
		listOfNumbers.add(6);
		listOfNumbers.add(7);
		listOfNumbers.add(8);
		
		System.out.println("Number traversed  from LinkedList");
		listOfNumbers.forEach(number -> System.out.println(number));
    }
}

Output: Number traversed from LinkedList
1
2
3
4
5
6
7
8

Leave a Comment