什么是函数式接口?

    函数式接口是一种特殊的接口,接口中只有一个抽象方法。

函数式接口与Lambda表达式有什么关系?

    当需要一个函数式接口的对象时,可以提供一个lambda表达式。

 

 1 package learnspringboot.xiao.learnjava;
 2 
 3 /**
 4  * @author xzy
 5  * @date 2019-11-17 15:24
 6  * 说明:函数式接口
 7  */
 8 public interface FunctionalInterface {
 9     void aFunction(Object... args);
10 }

 

 1 package learnspringboot.xiao;
 2 
 3 import learnspringboot.xiao.learnjava.FunctionalInterface;
 4 
 5 import java.util.ArrayList;
 6 import java.util.Arrays;
 7 import java.util.Comparator;
 8 import java.util.List;
 9 
10 /**
11  * @author 肖政宇
12  * @date 2019-10-23 16:22
13  * 说明:
14  */
15 public class MainFunction {
16 
17     public static void main(String[] args) {
18         FunctionalInterface functionalInterface = (objects) -> { 19             for (Object o : objects) { 20  System.out.println(o.toString()); 21  } 22 }; 23         functionalInterface.aFunction("123", "456", "aaa"); 24 
25         functionalInterface = (objects) -> { 26             double total = 0; 27             for (Object o : objects) { 28                 total += Double.valueOf(o.toString()); 29  } 30  System.out.println(total); 31  }; 32         functionalInterface.aFunction(1,2); 33     }
34 }