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