Scientech Easy How to Iterate LinkedList in Java | Scientech Easy

Monday, December 17, 2018

How to Iterate LinkedList in Java

In the last tutorial, we have discussed LinkedList and its various method with practical example programs. In this tutorial, we will learn How to iterate a LinkedList in Java. Before going on this topic, we recommend that you first learn Iterators in Java. There are five ways in which linked list can be iterated. They are as follows:
1.  For loop
2. Advanced For loop
3. While loop
4. Iterator
5. ListIterator
How to iterate LinkedList in Java, Java LinkedList Iterator Example Programs.

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

In this example, we have a linked list of String type. We will iterate using for loop, advanced for loop, and while loop.
Program source code 1:
    package linkedListEx; import java.util.LinkedList; public class LinkedListExample { 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?

In this example, we will iterate elements of the linked list using Iterator. We have a linked list of character type. Using Iterator, we can iterate the list in the forward direction only. The steps are given in the programs. 
Program source code 2:
    package linkedListEx; import java.util.Iterator; import java.util.LinkedList; public class LinkedListExample1 { public static void main(String[] args) { // Create a generic LinkedList object of String 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; System.out.println(obj); } } }
    Output: **Using Iterator** A B C D E

How to traverse a LinkedList using ListIterator?

In this example, 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 linkedListEx; import java.util.LinkedList; import java.util.ListIterator; public class LinkedListExample3 { 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; 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( at java.util.LinkedList$ListItr.previous( at linkedListEx.LinkedListExample2.main(
Final words 
We hope that this tutorial has covered Java LinkedList Iterator with example programs. All the practical example programs are very important to clear concepts of Iterator for beginners. Interviewer ask coding of the LinkedList Iterator.