使用一:
java可以通过Function、Consumer等接口的lambda表单式形式,提提取公共方法,把变化的部分的代码 放入lambda中,如下:
xxxxxx
aaaaaa
xxxxxx
xxxxxx
bbbbbb
xxxxxx
代码提取
public Result<User> loadRecords(Function<BpmInst,Boolean> function,List<Product>> query) {
xxxx
getxx(function.apply(xxx))
xxxx
}
loadRecords((BpmInst bpmInst) -> bpmInst.getAppId() == "D001"),query);
使用二:
还可以用来提取方法筛选条件,把条件提取到方法使用的时候使用,通过匿名内部类的形式传入,Function等方法lambda实现匿名内部类,如下:
/**
* @Param [bpmInstList, filterBpmInst]
* @return java.util.List<com.redxun.bpm.core.entity.BpmInst>
* @Description 筛选方法,把筛选条件提取成对象,筛选不在循环里实现,可以同时接口实现类,或者匿名内部类实现
* @author liuzonghua
* @date 2021/7/20 9:18
**/
public static List<BpmInst> filterBpmInstData(List<BpmInst> bpmInstList,FilterBpmInst filterBpmInst){
List<BpmInst> bpmInsts = new ArrayList<>();
for(BpmInst bpmInst:bpmInstList){
if(filterBpmInst.Test(bpmInst)){
bpmInsts.add(bpmInst);
}
}
return bpmInsts;
}
public static List<BpmInst> filterBpmInstData2(List<BpmInst> bpmInstList, Function<BpmInst,Boolean> function){
List<BpmInst> bpmInsts = new ArrayList<>();
for(BpmInst bpmInst:bpmInstList){
if(function.apply(bpmInst)){
bpmInsts.add(bpmInst);
}
}
return bpmInsts;
}
/**
* @Param
* @return
* @Description 筛选接口
* @author liuzonghua
* @date 2021/7/20 9:20
**/
@FunctionalInterface
public interface FilterBpmInst{
boolean Test(BpmInst bpmInst);
}
System.out.println(JSON.toJSONString(filterBpmInstData(nodeConfigList, new FilterBpmInst() {
@Override
public boolean Test(BpmInst bpmInst) {
if("G001".equals(bpmInst.getAppId())){
return true;
}
return false;
}
}),true));
//lambda表达式
System.out.println(JSON.toJSONString(filterBpmInstData(nodeConfigList,(BpmInst bpmInst) -> bpmInst.getAppId() == "G001")));
System.out.println(JSON.toJSONString(filterBpmInstData2(nodeConfigList,(BpmInst bpmInst) -> bpmInst.getAppId() == "D001")));
补充function等类的,andThen,compose方法,链式处理
接口内部实现如下:
/**
* Returns a composed function that first applies the {@code before}
* function to its input, and then applies this function to the result.
* If evaluation of either function throws an exception, it is relayed to
* the caller of the composed function.
*
* @param <V> the type of input to the {@code before} function, and to the
* composed function
* @param before the function to apply before this function is applied
* @return a composed function that first applies the {@code before}
* function and then applies this function
* @throws NullPointerException if before is null
*
* @see #andThen(Function)
*/
default <V> Function<V, R> compose(Function<? super V, ? extends T> before) {
Objects.requireNonNull(before);
return (V v) -> apply(before.apply(v));
}
/**
* Returns a composed function that first applies this function to
* its input, and then applies the {@code after} function to the result.
* If evaluation of either function throws an exception, it is relayed to
* the caller of the composed function.
*
* @param <V> the type of output of the {@code after} function, and of the
* composed function
* @param after the function to apply after this function is applied
* @return a composed function that first applies this function and then
* applies the {@code after} function
* @throws NullPointerException if after is null
*
* @see #compose(Function)
*/
default <V> Function<T, V> andThen(Function<? super R, ? extends V> after) {
Objects.requireNonNull(after);
return (T t) -> after.apply(apply(t));
}
例子:
Function<Integer,Integer> functionA = i->(i+1);
Function<Integer,Integer> functionB = i->(i*3);
可以看出来,a执行后b执行
System.out.println(functionA.andThen(functionB).apply(2));
同
System.out.println(functionB.apply(functionA.apply(2)));
b执行后a执行
System.out.println(functionA.compose(functionB).apply(2));
同
System.out.println(functionA.apply(functionB.apply(2)));