How to Iterate LinkedList in Java

In the last tutorial, we have discussed Java LinkedList and its various methods. In this tutorial, we will learn how to iterate LinkedList in Java.

Before going on this topic, I will recommend that you first learn the basics of LinkedList in Java then come on this topic.

There are five ways in which LinkedList can be iterated in Java. They are as follows:

1.  For loop
2. Advanced For loop
3. While loop
4. Iterator
5. ListIterator

Five ways to iterate LinkedList in Java

How to iterate LinkedList in Java using For loop, Advanced For loop, and While loop?


Let’s create a program where we will iterate LinkedList using for loop, advanced for loop, and while loop.

Program source code 1:
package iterateLinkedList; 
import java.util.LinkedList; 
public class LinkedListEx1 
{ 
public static void main(String[] args) 
{ 
// Create a generic LinkedList object of String type. 
   LinkedList<String> list = new LinkedList<String>(); 
// Adding elements in the list. 
    list.add("Red"); 
    list.add("Yellow"); 
    list.add("Green"); 
    list.add("White"); 
// Iterating using for loop. 
   System.out.println("**For loop**"); 
  for(int i = 0; i < list.size(); i++) 
  {
    Object element = list.get(i); // Return type of get() method is an Object. 
    System.out.println(element); 
} 
// Iterating using Advanced for loop. 
   System.out.println("**Advanced For loop**"); 
   for(String str: list) 
   { 
     System.out.println(str); 
   } 
// Iterating using while loop. 
   System.out.println("**While Loop**"); 
   int num = 0; 
  while (list.size() > num) 
  { 
    System.out.println(list.get(num)); 
     num++; 
   } 
 } 
}
Output: 
       **For loop** 
       Red Yellow Green White 
       **Advanced For loop** 
       Red Yellow Green White 
       **While Loop** 
       Red Yellow Green White

How to iterate Java LinkedList using Iterator?


Let’s take an example program where we will iterate elements of the linked list using Iterator. Using Iterator, we will iterate the list in the forward direction only. The steps are given in the programs. 
Program source code 2:
package iterateLinkedList; 
import java.util.Iterator; 
import java.util.LinkedList; 
public class LinkedListEx2 
{ 
public static void main(String[] args) 
{ 
// Create a generic LinkedList object of Character type. 
   LinkedList<Character> list = new LinkedList<Character>(); 
// Adding elements in the list. 
    list.add('A'); 
    list.add('B'); 
    list.add('C'); 
    list.add('D'); 
    list.add('E'); 

// Iterating using Iterator. 
   System.out.println("**Using Iterator**"); 
   Iterator<Character> itr = list.iterator(); 
  while(itr.hasNext())
  { 
   Object obj = itr.next(); 
   System.out.println(obj); 
   } 
  } 
}
Output: 
       **Using Iterator** 
       A B C D E

How to traverse LinkedList in Java using ListIterator?


Let’s take an example program where we will traverse or iterate elements of the LinkedList using ListIterator. We can iterate elements of the list in both forward as well backward direction. You can read more about it at ListIterator Javadoc
Program source code 3:
package iterateLinkedList; 
import java.util.LinkedList; 
import java.util.ListIterator; 
public class LinkedListEx3 
{ 
public static void main(String[] args) 
{ 
// Create a generic LinkedList object of type Integer. 
   LinkedList<Integer> list = new LinkedList<Integer>(); 
// Adding elements in the list. 
    list.add(10); 
    list.add(20); 
    list.add(30); 
    list.add(40); 
    list.add(50); 
  System.out.println("LinkedList original order"); 
  System.out.println(list); 
 ListIterator<Integer> litr = list.listIterator(); 
 System.out.println("Interating in forward direction"); 
 while(litr.hasNext())
 { 
   Object obj = litr.next(); 
   System.out.println(obj); 
  } 
  System.out.println("Iterating in backwrd direction"); 
  while(litr.hasPrevious())
  { 
    Object obj1 = litr.previous(); 
    System.out.println(obj1); 
     list.add(60); // It will throw Concurrent Modification Exception because we cannot add or remove element in the LinkedList during iteration. 
   } 
  System.out.println(list); 
 } 
 }
Output: 
       LinkedList original order 
       [10, 20, 30, 40, 50] 
       Interating in forward direction 
       10 20 30 40 50 
       Iterating in backwrd direction 
       50 
       java.util.ConcurrentModificationException at java.util.LinkedList$ListItr.checkForComodification(LinkedList.java:966) at 
java.util.LinkedList$ListItr.previous(LinkedList.java:903) at iterateLinkedList.LinkedListEx3.main(LinkedListEx3.java:31)
Final words 
Hope that this tutorial has covered how to iterate LinkedList in Java with example programs. I hope that you will have understood example programs and enjoyed them.
Thanks for reading!!!

Next ⇒ Java Set Interface⇐ PrevNext ⇒

Leave a Comment