Java Stream How to - Pass lambda function to method parameter








Question

We would like to know how to pass lambda function to method parameter.

Answer

//w w  w.  j a  v  a 2s  .  c  o  m
public class Main {

    public static void main(String[] args) {
        Runnable runnable = andThen(() -> System.out.print("Hello "), () -> System.out.print("World!"));
        new Thread(runnable).start();
    }
    
    static Runnable andThen (Runnable r1, Runnable r2) {
        return () -> {
            r1.run();
            r2.run();
        };
    }
}

The code above generates the following result.