Java Stream How to - Loop each item inside List with forEach method








Question

We would like to know how to loop each item inside List with forEach method.

Answer

import java.util.Arrays;
import java.util.List;
/*from  w  w w .  j av a 2 s  .co m*/
public class Main {
    public static void main(String[] args) {
        List<Integer> l = Arrays.asList(1, 3, 2, 4, 7, 8, 9, 6, 5);
        l.forEach(i -> { //Consumer is not even imported
            System.out.println(i + 10);
        });
    }   

}

The code above generates the following result.