CollectionUtils中的Transformer和Closure使用方法

Transformer相当于一个转换器,在java中也是一个接口,入参是一个object,出参是调用transform方法返回的一个object

Collectionutils中有2个相关方法是关于transformer的一个是collect,一个是transform,从调用方法上没看到二者有什么异常,个人理解是对空值的处理不同

        List<Integer> testList=new ArrayList<Integer>(){
            {
                add(1);
                add(2);
                add(3);
            }
        };        

        Collection resultTransform=CollectionUtils.collect(testList,i->{
            return "test"+i;
        });

        CollectionUtils.transform(testList,i->{
            return "test"+i;
        });

当让也可以使用transformerUtils直接使用现成的transform

        Collection resultTransformWithTransformer=CollectionUtils.collect(testList, TransformerUtils.nullTransformer());

 

Closure指的是闭包,也是一个名字叫Closure的接口,并通过实现excute的方法。CollectionUtils下有一个forAllDo的方法使用这个参数

使用的方法如下

        CollectionUtils.forAllDo(testList,i->{
            System.out.println(i);
            //并不会改变list
            i=0;
        });

ClosureUtils下还有一些常用的ifClosuse(二选一),whileClosure(先判断再执行),ChainedClosure(链式执行)

参考文档:https://commons.apache.org/proper/commons-collections/javadocs/api-3.2.2/org/apache/commons/collections/CollectionUtils.html

转载于:https://my.oschina.net/zimingforever/blog/1615875

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值