Java hutool List集合对象拷贝示例代码
作者:树欲静而风不止
这篇文章主要介绍了Java hutool List集合对象拷贝的相关资料,文章还分享了在实现过程中遇到的一些问题,并强调了阅读源码和正确配置CopyOptions的重要性,需要的朋友可以参考下
需求
三个对象,一个对象Point,一个对象CustomData。第三个对象IotDataCache用来屏蔽二者差异
public class Point extends Model<Point> {
@TableId
private Integer pointId;
private String iotCode;
}主键ID为整型
public class CustomData extends Model<CustomData> {
@Schema(description = "主键")
@TableId(type = IdType.ASSIGN_UUID)
private String id;
private String iotCode;
}
主键id为字符串

把Point的pointId 和 CustomData的id 都拷贝到IotDataCache中
iotCode字段不变
案例
public static void main(String[] args) {
List<Point> list1 = new ArrayList<>(1);
Point point = new Point();
point.setPointId(1);
point.setIotCode("root.test");
point.setName("123");
list1.add(point);
Point point2 = new Point();
point2.setPointId(2);
point2.setIotCode("root.test2");
point2.setName("123");
list1.add(point2);
Map<String,String> fieldMapping = new HashMap<>(1);
fieldMapping.put("pointId","id");
CopyOptions copyOptions = CopyOptions.create()
.setIgnoreNullValue(false)
.setFieldMapping(fieldMapping);
List<IotDataCache> temp = BeanUtil.copyToList(list1, IotDataCache.class, copyOptions);
System.out.println(temp);
List<CustomData> list2 = new ArrayList<>(2);
CustomData bean1 = new CustomData();
bean1.setId("customdata_h34fdjfsdouf9");
bean1.setIotCode("root.custom");
list2.add(bean1);
CustomData bean2 = new CustomData();
bean2.setId("customdata_h34fdjfsdouf8");
bean2.setIotCode("root.custom2");
list2.add(bean2);
List<IotDataCache> temp2 = BeanUtil.copyToList(list2, IotDataCache.class, null);
System.out.println(temp2);
}Test

完美实现
拓展
问了AI 很多东西牛头不对马尾的,方法名对不上,参数有问题啊什么的。看来还需要进步。还是得自己看源码
copyToList方法
/**
* 复制集合中的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) {
if (null == collection) {
return null;
}
if (collection.isEmpty()) {
return new ArrayList<>(0);
}
return collection.stream().map((source) -> {
final T target = ReflectUtil.newInstanceIfPossible(targetType);
copyProperties(source, target, copyOptions);
return target;
}).collect(Collectors.toList());
}CopyOptions配置
/**
* 属性拷贝选项<br>
* 包括:<br>
* 1、限制的类或接口,必须为目标对象的实现接口或父类,用于限制拷贝的属性,例如一个类我只想复制其父类的一些属性,就可以将editable设置为父类<br>
* 2、是否忽略空值,当源对象的值为null时,true: 忽略而不注入此值,false: 注入null<br>
* 3、忽略的属性列表,设置一个属性列表,不拷贝这些属性值<br>
*
* @author Looly
*/
public class CopyOptions implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 限制的类或接口,必须为目标对象的实现接口或父类,用于限制拷贝的属性,例如一个类我只想复制其父类的一些属性,就可以将editable设置为父类<br>
* 如果目标对象是Map,源对象是Bean,则作用于源对象上
*/
protected Class<?> editable;
/**
* 是否忽略空值,当源对象的值为null时,true: 忽略而不注入此值,false: 注入null
*/
protected boolean ignoreNullValue;
/**
* 属性过滤器,断言通过的属性才会被复制<br>
* 断言参数中Field为源对象的字段对象,如果源对象为Map,使用目标对象,Object为源对象的对应值
*/
private BiPredicate<Field, Object> propertiesFilter;
/**
* 是否忽略字段注入错误
*/
protected boolean ignoreError;
/**
* 是否忽略字段大小写
*/
protected boolean ignoreCase;
/**
* 字段属性编辑器,用于自定义属性转换规则,例如驼峰转下划线等<br>
* 规则为,{@link Editor#edit(Object)}属性为源对象的字段名称或key,返回值为目标对象的字段名称或key
*/
private Editor<String> fieldNameEditor;
/**
* 字段属性值编辑器,用于自定义属性值转换规则,例如null转""等
*/
protected BiFunction<String, Object, Object> fieldValueEditor;
/**
* 是否支持transient关键字修饰和@Transient注解,如果支持,被修饰的字段或方法对应的字段将被忽略。
*/
protected boolean transientSupport = true;
/**
* 是否覆盖目标值,如果不覆盖,会先读取目标对象的值,非{@code null}则写,否则忽略。如果覆盖,则不判断直接写
*/
protected boolean override = true;
/**
* 自定义类型转换器,默认使用全局万能转换器转换
*/
protected TypeConverter converter = (type, value) ->
Convert.convertWithCheck(type, value, null, ignoreError);
//region create
/**
* 创建拷贝选项
*
* @return 拷贝选项
*/
public static CopyOptions create() {
return new CopyOptions();
}
/**
* 创建拷贝选项
*
* @param editable 限制的类或接口,必须为目标对象的实现接口或父类,用于限制拷贝的属性
* @param ignoreNullValue 是否忽略空值,当源对象的值为null时,true: 忽略而不注入此值,false: 注入null
* @param ignoreProperties 忽略的属性列表,设置一个属性列表,不拷贝这些属性值
* @return 拷贝选项
*/
public static CopyOptions create(Class<?> editable, boolean ignoreNullValue, String... ignoreProperties) {
return new CopyOptions(editable, ignoreNullValue, ignoreProperties);
}
//endregion
/**
* 构造拷贝选项
*/
public CopyOptions() {
}
/**
* 构造拷贝选项
*
* @param editable 限制的类或接口,必须为目标对象的实现接口或父类,用于限制拷贝的属性
* @param ignoreNullValue 是否忽略空值,当源对象的值为null时,true: 忽略而不注入此值,false: 注入null
* @param ignoreProperties 忽略的目标对象中属性列表,设置一个属性列表,不拷贝这些属性值
*/
public CopyOptions(Class<?> editable, boolean ignoreNullValue, String... ignoreProperties) {
this.propertiesFilter = (f, v) -> true;
this.editable = editable;
this.ignoreNullValue = ignoreNullValue;
this.setIgnoreProperties(ignoreProperties);
}
/**
* 设置限制的类或接口,必须为目标对象的实现接口或父类,用于限制拷贝的属性
*
* @param editable 限制的类或接口
* @return CopyOptions
*/
public CopyOptions setEditable(Class<?> editable) {
this.editable = editable;
return this;
}
/**
* 设置是否忽略空值,当源对象的值为null时,true: 忽略而不注入此值,false: 注入null
*
* @param ignoreNullVall 是否忽略空值,当源对象的值为null时,true: 忽略而不注入此值,false: 注入null
* @return CopyOptions
*/
public CopyOptions setIgnoreNullValue(boolean ignoreNullVall) {
this.ignoreNullValue = ignoreNullVall;
return this;
}
/**
* 设置忽略空值,当源对象的值为null时,忽略而不注入此值
*
* @return CopyOptions
* @since 4.5.7
*/
public CopyOptions ignoreNullValue() {
return setIgnoreNullValue(true);
}
/**
* 属性过滤器,断言通过的属性才会被复制<br>
* {@link BiPredicate#test(Object, Object)}返回{@code true}则属性通过,{@code false}不通过,抛弃之
*
* @param propertiesFilter 属性过滤器
* @return CopyOptions
*/
public CopyOptions setPropertiesFilter(BiPredicate<Field, Object> propertiesFilter) {
this.propertiesFilter = propertiesFilter;
return this;
}
/**
* 设置忽略的目标对象中属性列表,设置一个属性列表,不拷贝这些属性值
*
* @param ignoreProperties 忽略的目标对象中属性列表,设置一个属性列表,不拷贝这些属性值
* @return CopyOptions
*/
public CopyOptions setIgnoreProperties(String... ignoreProperties) {
return setPropertiesFilter((field, o) -> false == ArrayUtil.contains(ignoreProperties, field.getName()));
}
/**
* 设置忽略的目标对象中属性列表,设置一个属性列表,不拷贝这些属性值,Lambda方式
*
* @param <P> 参数类型
* @param <R> 返回值类型
* @param funcs 忽略的目标对象中属性列表,设置一个属性列表,不拷贝这些属性值
* @return CopyOptions
* @since 5.8.0
*/
@SuppressWarnings("unchecked")
public <P, R> CopyOptions setIgnoreProperties(Func1<P, R>... funcs) {
final Set<String> ignoreProperties = ArrayUtil.mapToSet(funcs, LambdaUtil::getFieldName);
return setPropertiesFilter((field, o) -> false == ignoreProperties.contains(field.getName()));
}
/**
* 设置是否忽略字段的注入错误
*
* @param ignoreError 是否忽略注入错误
* @return CopyOptions
*/
public CopyOptions setIgnoreError(boolean ignoreError) {
this.ignoreError = ignoreError;
return this;
}
/**
* 设置忽略字段的注入错误
*
* @return CopyOptions
* @since 4.5.7
*/
public CopyOptions ignoreError() {
return setIgnoreError(true);
}
/**
* 设置是否忽略字段的大小写
*
* @param ignoreCase 是否忽略大小写
* @return CopyOptions
*/
public CopyOptions setIgnoreCase(boolean ignoreCase) {
this.ignoreCase = ignoreCase;
return this;
}
/**
* 设置忽略字段的大小写
*
* @return CopyOptions
* @since 4.5.7
*/
public CopyOptions ignoreCase() {
return setIgnoreCase(true);
}
/**
* 设置拷贝属性的字段映射,用于不同的属性之前拷贝做对应表用
*
* @param fieldMapping 拷贝属性的字段映射,用于不同的属性之前拷贝做对应表用
* @return CopyOptions
*/
public CopyOptions setFieldMapping(Map<String, String> fieldMapping) {
return setFieldNameEditor((key -> fieldMapping.getOrDefault(key, key)));
}
/**
* 设置字段属性编辑器,用于自定义属性转换规则,例如驼峰转下划线等<br>
* 此转换器只针对源端的字段做转换,请确认转换后与目标端字段一致<br>
* 当转换后的字段名为null时忽略这个字段
*
* @param fieldNameEditor 字段属性编辑器,用于自定义属性转换规则,例如驼峰转下划线等
* @return CopyOptions
* @since 5.4.2
*/
public CopyOptions setFieldNameEditor(Editor<String> fieldNameEditor) {
this.fieldNameEditor = fieldNameEditor;
return this;
}
/**
* 设置字段属性值编辑器,用于自定义属性值转换规则,例如null转""等<br>
*
* @param fieldValueEditor 字段属性值编辑器,用于自定义属性值转换规则,例如null转""等
* @return CopyOptions
* @since 5.7.15
*/
public CopyOptions setFieldValueEditor(BiFunction<String, Object, Object> fieldValueEditor) {
this.fieldValueEditor = fieldValueEditor;
return this;
}
/**
* 编辑字段值
*
* @param fieldName 字段名
* @param fieldValue 字段值
* @return 编辑后的字段值
* @since 5.7.15
*/
protected Object editFieldValue(String fieldName, Object fieldValue) {
return (null != this.fieldValueEditor) ?
this.fieldValueEditor.apply(fieldName, fieldValue) : fieldValue;
}
/**
* 设置是否支持transient关键字修饰和@Transient注解,如果支持,被修饰的字段或方法对应的字段将被忽略。
*
* @param transientSupport 是否支持
* @return this
* @since 5.4.2
*/
public CopyOptions setTransientSupport(boolean transientSupport) {
this.transientSupport = transientSupport;
return this;
}
/**
* 设置是否覆盖目标值,如果不覆盖,会先读取目标对象的值,非{@code null}则写,否则忽略。如果覆盖,则不判断直接写
*
* @param override 是否覆盖目标值
* @return this
* @since 5.7.17
*/
public CopyOptions setOverride(boolean override) {
this.override = override;
return this;
}
/**
* 设置自定义类型转换器,默认使用全局万能转换器转换。
*
* @param converter 转换器
* @return this
* @since 5.8.0
*/
public CopyOptions setConverter(TypeConverter converter) {
this.converter = converter;
return this;
}
/**
* 使用自定义转换器转换字段值<br>
* 如果自定义转换器为{@code null},则返回原值。
*
* @param targetType 目标类型
* @param fieldValue 字段值
* @return 编辑后的字段值
* @since 5.8.0
*/
protected Object convertField(Type targetType, Object fieldValue) {
return (null != this.converter) ?
this.converter.convert(targetType, fieldValue) : fieldValue;
}
/**
* 转换字段名为编辑后的字段名
*
* @param fieldName 字段名
* @return 编辑后的字段名
* @since 5.4.2
*/
protected String editFieldName(String fieldName) {
return (null != this.fieldNameEditor) ? this.fieldNameEditor.edit(fieldName) : fieldName;
}
/**
* 测试是否保留字段,{@code true}保留,{@code false}不保留
*
* @param field 字段
* @param value 值
* @return 是否保留
*/
protected boolean testPropertyFilter(Field field, Object value) {
return null == this.propertiesFilter || this.propertiesFilter.test(field, value);
}
}
常用的可能就是Null忽略情况,大小写情况、忽略字段、属性字段映射(其实就是fieldNameEditor)。各位成功!
总结
到此这篇关于Java hutool List集合对象拷贝的文章就介绍到这了,更多相关Java hutool List集合对象拷贝内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
