SAM Interfaces
Single Abstract Method interfaces
have only one method declared in their interface definition.
类似:
java.lang.Runnable
java.awt.event.ActionListener
java.util.Comparator
java.util.concurrent.Callable
Functional interfaces
represented using using Lambda expressions
例子
DataProvider: 负责维护了一个map的数据
CalcService: 负责对数据的计算和更新
写法一, 以下的问题在于, DataProvider 不应该暴露了数据结构给外部, 导致数据更新会失控.
void CalcService.update(){
Map mapInfo = DataProvider.getMap();
for(mapItem:mapInfo ){
rpcGet();
calc(mapItem);
mapItem.put(mapItem.getKey(),valueNew);
}
}
写法二, 使用Consumer, Lambda
void CalcService.update(){
DataProvider.forEach(mapItem->{
rpcGet();
calc(mapItem);
mapItem.put(mapItem.getKey(),valueNew);
}
}
几个函数接口
Function : 接口有一个返回值
Consumer : 接口没有返回值
Predicate : 主要 判断 输入的对象是否 符合某个条件