package com.suning.cpm.utils;
import lombok.extern.slf4j.Slf4j;
import net.sf.cglib.beans.BeanCopier;
import org.apache.commons.collections.CollectionUtils;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
/**
* @author 20014258
* @date 2021/1/19
*/
@Slf4j
public class GeneralUtils {
/**
* 多集合(过滤空集合)取交集retain公共方法
*/
public static Collection retain(Collection<Collection> c){
Optional result = c.parallelStream()
.filter(element -> CollectionUtils.isNotEmpty(element))
.reduce((m1, m2)->{
m1.retainAll(m2);
return m1;
});
return (Collection) result.get();
}
/**
* 多集合(过滤空集合)取交集retain公共方法 参数String
*/
public static List<String> retainElementList(List<List<String>> elementLists) {
Optional<List<String>> result = elementLists.parallelStream()
.filter(elementList -> CollectionUtils.isNotEmpty(elementList))
.reduce((a, b) -> {
a.retainAll(b);
return a;
});
return result.orElse(new ArrayList<>());
}
/**
* 性能更好的对象拷贝属性
* 要比 BeanUtils.copyProperties 快n倍
* @param source
* @param target
*/
public static void copyProperties(Object source, Object target) {
BeanCopier copier = BeanCopier.create(source.getClass(), target.getClass(), false);
copier.copy(source, target, null);
}
}
原文:https://www.cnblogs.com/wszn-java/p/14754705.html