总结之Function、Consumer等接口使用

本文详细介绍了Java中如何使用Lambda表达式和Function接口来提取公共方法和筛选条件,以提高代码复用性和可读性。通过示例展示了如何使用匿名内部类和lambda简化代码,并解释了`andThen`和`compose`方法,用于实现函数的链式处理。
摘要由CSDN通过智能技术生成

使用一:

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)));
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值