Various ways to iterate through LinkedList – 5 ways

In this article, we will discuss various ways to iterate through LinkedList – 5 ways

Various ways to iterate through LinkedList

  1. Regular for-loop
  2. Enhanced for-loop introduced in Java 1.5 version
  3. Iterating using Iterator of Collection interface
  4. Iterating using ListIterator of List interface
  5. forEach() loop introduced in Java 1.8 version

 

Example: Various ways to iterate through LinkedList

DifferentWaysToIterateLinkedList.java

package in.bench.resources.collection;

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

public class DifferentWaysToIterateLinkedList {

	public static void main(String[] args) {

		// creating LinkedList object of type String
		LinkedList<String> ll = new LinkedList<String>();

		// adding elements to LinkedList object
		ll.add("Samsung");
		ll.add("iPhone");  
		ll.add("OnePlus");  
		ll.add("Motorola");
		ll.add("iPhone"); // duplicate object
		ll.add("Micromax");
		ll.add(null); // null is allowed


		// Way 1: Iterating using regular for-loop
		System.out.println("Way 1: Iterating using"
				+ " Regular for-loop\n");
		for(int index = 0; index < ll.size(); index++) {
			System.out.println(ll.get(index));
		}

		// Way 2: Iterating using enhanced for-each loop
		System.out.println("\n\nWay 2: Iterating using"
				+ " enhanced for-each loop\n");
		for(String str : ll) {
			System.out.println(str);
		}

		// Way 3: Iterating using Iterator of Collection interface
		System.out.println("\n\nWay 3: Iterating using"
				+ " Iterator of Collection interface\n");
		Iterator<String> itr = ll.iterator();
		while(itr.hasNext()) {
			System.out.println(itr.next());
		}

		// Way 4: Iterating using ListIterator of List interface
		System.out.println("\n\nWay 4.A: Iterating using"
				+ " ListIterator of List interface in"
				+ " FORWARD direction\n");
		ListIterator<String> lstItr = ll.listIterator();
		while(lstItr.hasNext()) {
			System.out.println(lstItr.next());
		}

		System.out.println("\n\nWay 4.B: Iterating using"
				+ " ListIterator of List interface in"
				+ " BACKWARD direction\n");
		while(lstItr.hasPrevious()) {
			System.out.println(lstItr.previous());
		}

		// Way 5: Iterating using forEach loop in Java 1.8
		System.out.println("\n\nWay 5: Iterating using"
				+ " forEach loop in Java 1.8\n");
		ll.forEach(fone -> System.out.println(fone));
	}

}

Output:

Way 1: Iterating using Regular for-loop

Samsung
iPhone
OnePlus
Motorola
iPhone
Micromax
null


Way 2: Iterating using enhanced for-each loop

Samsung
iPhone
OnePlus
Motorola
iPhone
Micromax
null


Way 3: Iterating using Iterator of Collection interface

Samsung
iPhone
OnePlus
Motorola
iPhone
Micromax
null


Way 4.A: Iterating using ListIterator of List interface in FORWARD direction

Samsung
iPhone
OnePlus
Motorola
iPhone
Micromax
null


Way 4.B: Iterating using ListIterator of List interface in BACKWARD direction

null
Micromax
iPhone
Motorola
OnePlus
iPhone
Samsung


Way 5: Iterating using forEach loop in Java 1.8

Samsung
iPhone
OnePlus
Motorola
iPhone
Micromax
null

From above example, LinkedList

  • allows duplicate elements
  • null object is allowed
  • while iterating insertion-order is maintained

 

References:

 

Happy Coding !!
Happy Learning !!

Various ways to iterate through TreeSet – 3 ways
How to find duplicate in String Arrays ?