Java 8 pass method as parameter

后端 未结 2 1175
自闭症患者
自闭症患者 2020-12-23 15:54

Currently getting into Java 8 lambda expressions and method references.

I want to pass a method with no args and no return value as argument to another method. This

相关标签:
2条回答
  • 2020-12-23 16:33

    You can also pass lambda like this:

    public void pass() {
        run(()-> System.out.println("Hello world"));
    }
    
    public void run(Runnable function) {
        function.run();
    }
    

    In this way, you are passing lambda directly as method.

    0 讨论(0)
  • 2020-12-23 16:39

    It really does not matter; Runnable will do too.

    Consumer<Void>,
    Supplier<Void>,
    Function<Void, Void>
    
    0 讨论(0)
提交回复
热议问题