How to Traverse ArrayList using forEach() method in Java
Tags: ArrayList
In this Java core tutorial we learn how to traverse through all element of an java.util.ArrayList using the forEach() method in Java programming language.
How to traverse through an ArrayList using forEach() method in Java
In the following Java program, we show how to traverse through all elements of an ArrayList using the Iterable.forEach(Consumer<? super T> action) method.
ArrayListForEachMethodExample1.java
import java.util.ArrayList;
import java.util.List;
public class ArrayListForEachMethodExample1 {
public static void main(String... args) {
List<String> list = new ArrayList<>();
list.add("One");
list.add("Two");
list.add("Three");
// For each over all elements of an ArrayList using forEach() method
list.forEach(System.out::println);
}
}
One
Two
Three
Happy Coding 😊
Related Articles
Java Convert Array to ArrayList
Java Insert Element to ArrayList at Specified Index
Java Get Minimum Value in ArrayList
How to Traverse ArrayList using Iterator in Java
How to Traverse ArrayList using for each loop in Java
Java Convert ArrayList to LinkedList
Java Convert ArrayList to Array
Java Convert ArrayList to String
Java Convert ArrayList to Comma Separated String
Java Convert ArrayList to HashSet
Java Create New Array with Class Type and Length
Java Create ArrayList using List Interface