Java 8 – Iterate through List

In this example, We will show you simple example about, How to Iterate through list example in Java 8. The example has been tested and output shared in the same post.

Example Program

package com.dineshkrish;
import java.util.ArrayList;
import java.util.List;
/**
* 
* @author Dinesh Krishnan
*
*/
public class ListForEach {
public static void main(String[] args) {
// defining the array list
List<String> numbers = new ArrayList<String>();
// adding the elements to list
numbers.add("One");
numbers.add("Two");
numbers.add("Three");
numbers.add("Four");
numbers.add("Five");
numbers.add("Six");
numbers.add("Seven");
numbers.add("Eight");
numbers.add("Nine");
numbers.add("Ten");
// iterating the elements over the list
numbers.forEach(number -> System.out.println(number));
}
}

Output

One
Two
Three
Four
Five
Six
Seven
Eight
Nine
Ten

References

1. JavaDoc – List Interface
2. JavaDoc – ArrayList Class
3. Java 8 – Iterable forEach() method

No responses yet

Leave a Reply

Your email address will not be published. Required fields are marked *