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);
    }
}
The output as below.
One
Two
Three

Happy Coding 😊

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 Add Element to ArrayList

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

Convert Array to List in Java

Java Create New Array with Class Type and Length

Java Create ArrayList using List Interface

Java Get Index of Minimum Value in ArrayList

How to use Java ArrayList with Examples