What are functional interfaces used for in Java 8?

前端 未结 10 2121
Happy的楠姐
Happy的楠姐 2020-11-22 09:08

I came across a new term in Java 8: "functional interface". I could only find one use of it while working with lambda expressions.

Java 8 provides

10条回答
  •  清酒与你
    2020-11-22 09:44

    You can use lambda in Java 8

    public static void main(String[] args) {
        tentimes(inputPrm - > System.out.println(inputPrm));
        //tentimes(System.out::println);  // You can also replace lambda with static method reference
    }
    
    public static void tentimes(Consumer myFunction) {
        for (int i = 0; i < 10; i++)
            myFunction.accept("hello");
    }
    

    For further info about Java Lambdas and FunctionalInterfaces

提交回复
热议问题