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:
- Traditional For Loop
- Enhanced For Loop
- ForEach Lambda Expression
- Java 8 Streams
- Iterator / ListIterator
1. Iterate using ‘Traditional For Loop’
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | /** * 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)); } } } |
1 2 3 4 5 6 | ------- List Iteration using Traditional For Loop ------- Instagram Facebook Snapchat Twitter Tumblr |
2. Iterate using ‘Enhanced For Loop’
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | /** * 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); } } } |
1 2 3 4 5 6 | ------- List Iteration using Enhanced For Loop ------- Instagram Facebook Snapchat Twitter Tumblr |
3. Iterate using ‘ForEach Lambda Expression’
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | /** * 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); }); } } |
1 2 3 4 5 6 | ------- List Iteration using ForEach Lambda Expression ------- Instagram Facebook Snapchat Twitter Tumblr |
4. Iterate using ‘Java 8 Streams’
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | /** * 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)); } } |
1 2 3 4 5 6 | ------- List Iteration using Java 8 Streams ------- Instagram Facebook Snapchat Twitter Tumblr |
5. Iterate using ‘Iterator / ListIterator’
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | /** * 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()); } } } |
1 2 3 4 5 6 | ------- List Iteration using Traditional For Loop ------- Instagram Facebook Snapchat Twitter Tumblr |
Learn 5 Unique ways to ITERATE a LIST in Java...!!! Share on X
Do you like this Post? – then check my other helpful posts:
- Passing Function as a Parameter in another Method in Java 8
- Collection sorting using Lambda in Java 8
- Generate Prime numbers in Java 8
- Java 8 program to calculate average of N numbers
- Reverse a word in a string in Java 8
- Finding prime number using Java 8
- Double the numbers of specified ArrayList using Streams