函数的真谛
1 2 3 4 5 6 7 8 9 10 11 12
| interface Promise{ boolean is(a,b) }
fun getByCondition(Promise pm){ if(pm.is(a,b)) }
//调用 getByCondition( (a,b) -> (xxx) )
|
logger中的
interface Supplier{
T get();
}
在软件开发中,”interface supplier” 通常指的是 提供特定类型对象或数据的接口。 它可以被理解为一个 提供者,其主要功能是生成或获取某种类型的实例,而不需要调用者关心具体的创建过程。 在Java 8及以后的版本中,java.util.function.Supplier<T>接口就扮演着这样的角色,它代表了一个不接受参数但返回一个值(类型为T)的函数。
函数对象表现形式
lambda表达式
1 2 3
| // Lambda 表达式 Predicate<String> startsWithA = s -> s.startsWith("A"); System.out.println(startsWithA.test("Apple")); // 输出 true
|

方法引用
1 2 3
| // 方法引用 Predicate<String> startsWithA_MethodRef = String::startsWith; System.out.println(startsWithA_MethodRef.test("Banana", "B")); // 输出 false
|
函数对象类型
参数个数类型相同,返回值类型相同 -> 函数式接口,只包含一个抽象方法,用@FunctionallInterface
自定义函数
1 2 3 4 5 6 7 8
| //编译期检查,是否只有一个抽象方法 @FunctionInterface interface Type{ boolean op(int a) } main{ Type type= a-> xxx }
|
jdk函数




闭包
限制:参数以外的数据必须是final or effictive final

柯里化
Optional一条龙
Optional用法与争议点 - 扣钉日记 - 博客园
1 2 3 4 5 6
| public String getCity(User user) throws Exception{ return Optional.ofNullable(user) .map(u-> u.getAddress()) .map(a->a.getCity()) .orElseThrow(()->new Exception("取指错误")); }
|