mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
add copyToList
This commit is contained in:
@@ -29,6 +29,7 @@ import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* Bean工具类
|
||||
@@ -714,6 +715,25 @@ public class BeanUtil {
|
||||
BeanCopier.create(source, target, copyOptions).copy();
|
||||
}
|
||||
|
||||
/**
|
||||
* 复制集合中的Bean属性<br>
|
||||
* 此方法遍历集合中每个Bean,复制其属性后加入一个新的{@link List}中。
|
||||
*
|
||||
* @param collection 原Bean集合
|
||||
* @param targetType 目标Bean类型
|
||||
* @param copyOptions 拷贝选项
|
||||
* @param <T> Bean类型
|
||||
* @return 复制后的List
|
||||
* @since 5.6.4
|
||||
*/
|
||||
public static <T> List<T> copyToList(Collection<?> collection, Class<T> targetType, CopyOptions copyOptions){
|
||||
return collection.stream().map((source)->{
|
||||
final T target = ReflectUtil.newInstanceIfPossible(targetType);
|
||||
copyProperties(source, target, copyOptions);
|
||||
return target;
|
||||
}).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
/**
|
||||
* 给定的Bean的类名是否匹配指定类名字符串<br>
|
||||
* 如果isSimple为{@code false},则只匹配类名而忽略包名,例如:cn.hutool.TestEntity只匹配TestEntity<br>
|
||||
@@ -845,4 +865,5 @@ public class BeanUtil {
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user