需求
三个对象,一个对象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集合对象拷贝内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论