函数式接口
1.函数式接口
只能含有1个方法的接口,入参可以有多个,出参可有可无
定义接口
@FunctionalInterface
public interface ReceiverGetter {
List<Receiver> apply(ResGroup resGroup);
}
使用策略模式使用接口
Map<String,ReceiverGetter> map = Maps.newHashMap();
map.put("DBA",resgroup -> return Lists.newArrayList())
调用策略模式中定义的函数方法去执行
map.get("DBA").apply(resGroup)
2.Supplier接口
java.util.function.Supplier
定义一个Supplier接口的通过无参方法返回字符串helloworld
Supplier<String> supplier = new Supplier<String>(){
@Override
public String get(){
return "helloworld"
}
}
策略模式使用Supplier接口
Map<String,String> map = Maps.newHashMap();
map.put("DBA",() -> return "helloworld")
使用策略模式
String ss = map.get("DBA").get()
3.Consumer接口
Consumer接口正好相反,不是生产一个数据,而是消费一个数据
常用与传递参数进行处理.没有返回值
策略模式中,value值是Consumer接口,Consumer接口是处理ProcessContext<NotificationModel>>这种数据类型
Map<ResGroupType, Consumer<ProcessContext<NotificationModel>>> rewriteActions = new ImmutableMap.Builder<ResGroupType,Consumer<ProcessContext<NotificationModel>>>()
.put(ResGroupType.CONFIG_RES_GROUP,context -> rewriteConfigResGroupSetting(context))
.put(ResGroupType.VIRTUAL_RES_GROUP,context -> rewriteVirtualResGroupSetting(context))
.build();
处理
rewriteActions.get(resGroupType).accept(context);
4.Predicate接口
过滤接口,抽象方法test
5.Function接口
java.util.function.Function<T,R> 接口来根据一个类型的数据,得到另一个类型的数据
前置条件 -> 后置条件
6.内置函数式接口的子类

原创:做时间的朋友

浙公网安备 33010602011771号