Java Stream How to - Pass in static method to List forEach method








Question

We would like to know how to pass in static method to List forEach method.

Answer

import java.util.Arrays;
import java.util.List;
public class Main {
/*from  w  w  w .  j ava  2  s. co  m*/
    public static void main(String[] args) {
        List<Integer> l = Arrays.asList(1, 3, 2, 4, 7, 8, 9, 6, 5);
        l.forEach(Main::methodWithSameInput);
    }   
    
    public static int methodWithSameInput(Integer i ){
        System.out.println(i + 10);
        return 10; //Even it returns int, it still work 
    }

}

The code above generates the following result.