函数式接口
1、只包含一个抽象方法的接口,称为函数式接口。
2、你可以通过Lambda 表达式来创建该接口的对象。(若Lambda 表达式抛出一个受检异常,那么该异常需要在目标接口的抽象方法上进行声明)。
3、我们可以在任意函数式接口上使用@FunctionalInterface注解,这样做可以检查它是否是一个函数式接口,同时javadoc也会包含一条声明,说明这个接口是一个函数式接口。
自定义函数式接口
@FunctionalInterface public interface Myinterface { public String getResul(); } // 函数式接口中使用泛型 @FunctionalInterface public interface Myinterface<T> { public T getResul(T t); }
作为参数传递 Lambda 表达式
public String toUpperString(Myinterface<String> mf, String str) { return mf.getResul(str); } public void test() { //作为参数传递 Lambda 表达式 String nStr = toUpperString( (str) -> str.toLowerCase(), "asdfgh"); // 作为参数传递 Lambda 表达式:为了将 Lambda 表达式作为参数传递, // 接收Lambda 表达式的参数类型必须与该Lambda 表达式兼容的函数式接口的类in个 }
Java 内置四大核心函数式接口
其他接口