Java introduced numerous ways to achieve a particular coding objective and, this is what we are going to learn and take advantage of to ‘Iterate through List’ using various techniques. You can pick any approach to use in your projects. All approaches have their own advantages.

5 Unique ways to Iterate through a LIST in Java

Check out: takeWhile / dropWhile operations in Java 9 Stream

Learn 5 Unique ways to ITERATE a LIST in Java...!!! Share on X

In this tutorial, we are going to use below approaches to Iterate a List:

  1. Traditional For Loop
  2. Enhanced For Loop
  3. ForEach Lambda Expression
  4. Java 8 Streams
  5. Iterator / ListIterator

1. Iterate using ‘Traditional For Loop’

/**
 * Iterate through List using Traditional For Loop in Java
 * @author Deepak Verma
 */

import java.util.Arrays;
import java.util.List;

public class Iterate_List_using_Traditional_For_Loop {

	public static void main(String args[]) {
		  
        //Create a List and Add 5 Items to the it
        List<String> socialmediaList = Arrays.asList("Instagram", "Facebook", "Snapchat", "Twitter", "Tumblr");
 
        //Iterate through List Items using Traditional For loop
        System.out.println("------- List Iteration using Traditional For Loop -------");
        for (int i = 0; i < socialmediaList.size(); i++) {
            System.out.println(socialmediaList.get(i));
        }
 
 }
}

Output:

------- List Iteration using Traditional For Loop -------
Instagram
Facebook
Snapchat
Twitter
Tumblr

2. Iterate using ‘Enhanced For Loop’

/**
 * Iterate through List using Enhanced For Loop in Java
 * @author Deepak Verma
 */

import java.util.Arrays;
import java.util.List;

public class Iterate_List_using_Enhanced_For_Loop {

	public static void main(String args[]) {
		  
        //Create a List and Add 5 Items to the List
        List<String> socialmediaList = Arrays.asList("Instagram", "Facebook", "Snapchat", "Twitter", "Tumblr");
 
        //Iterate List Items using Enhanced For loop
        System.out.println("------- List Iteration using Enhanced For Loop -------");
        for (String item : socialmediaList) {
            System.out.println(item);
        }
 
 }
}

Output:

------- List Iteration using Enhanced For Loop -------
Instagram
Facebook
Snapchat
Twitter
Tumblr


3. Iterate using ‘ForEach Lambda Expression’

/**
 * Iterate through List using ForEach Lambda Expression in Java
 * @author Deepak Verma
 */

import java.util.Arrays;
import java.util.List;

public class Iterate_List_using_ForEach_Lambda_Expression {

	public static void main(String args[]) {
		  
        //Create a List and Add 5 Items to the List
        List<String> socialmediaList = Arrays.asList("Instagram", "Facebook", "Snapchat", "Twitter", "Tumblr");
 
        //Iterate List Items using ForEach Lambda Expression
        System.out.println("------- List Iteration using ForEach Lambda Expression -------");
        socialmediaList.forEach((item) -> {
            System.out.println(item);
        });
 
 }
}

Output:

------- List Iteration using ForEach Lambda Expression -------
Instagram
Facebook
Snapchat
Twitter
Tumblr



4. Iterate using ‘Java 8 Streams’

/**
 * Iterate through List using Java 8 Streams in Java
 * @author Deepak Verma
 */

import java.util.Arrays;
import java.util.List;

public class Iterate_List_using_Java8_Streams {

	public static void main(String args[]) {
		  
        //Create a List and Add 5 Items to the List
        List<String> socialmediaList = Arrays.asList("Instagram", "Facebook", "Snapchat", "Twitter", "Tumblr");
 
        //Iterate List Items using Java 8 Streams
        System.out.println("------- List Iteration using Java 8 Streams -------");
        socialmediaList.stream()
                       .forEach((item) -> System.out.println(item));
 
 }
}

Output:

------- List Iteration using Java 8 Streams -------
Instagram
Facebook
Snapchat
Twitter
Tumblr



5. Iterate using ‘Iterator / ListIterator’

/**
 * Iterate through List using Iterator / ListIterator in Java
 * @author Deepak Verma
 */

import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

public class Iterate_List_using_Iterator_ListIterator {

	public static void main(String args[]) {
		  
        //Create a List and Add 5 Items to the List
        List<String> socialmediaList = Arrays.asList("Instagram", "Facebook", "Snapchat", "Twitter", "Tumblr");
 
        //Iterate List Items using Iterator
        System.out.println("------- List Iteration using Iterator -------");
        Iterator<String> socialmediaIterator = socialmediaList.iterator();
        while (socialmediaIterator.hasNext()) {
            System.out.println(socialmediaIterator.next());
        }
 
        //Iterate List Items using ListIterator
        System.out.println("------- List Iteration using ListIterator -------");
        ListIterator<String> socialmediaListIterator = socialmediaList.listIterator();
        while (socialmediaListIterator.hasNext()) {
            System.out.println(socialmediaListIterator.next());
        }
 }
}

Output:

------- List Iteration using Traditional For Loop -------
Instagram
Facebook
Snapchat
Twitter
Tumblr


In programming, List Iteration is a very common phenomenon and it’s very rare that you are not going to encounter a scenario in your programming career where you need to use the List Iteration and how wonderful and useful it is to know various ways of doing it. Isn’t it? Well, Having said that, I hope above examples could help you to get a better understanding on various approaches on implementing the List Iteration in Java.

Learn 5 Unique ways to ITERATE a LIST in Java...!!! Share on X

Do you like this Post? – then check my other helpful posts:

Other Useful References:

Author

  • Deepak Verma

    Deepak Verma is a Test Automation Consultant and Software development Engineer for more than 10 years. His mission is to help you become an In-demand full stack automation tester.

    He is also the founder of Techndeck, a blog and online coaching platform dedicated to helping you succeed with all the automation basics to advanced testing automation tricks.

    View all posts