Java hutool List集合对象拷贝

需求

三个对象,一个对象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 很多东西牛头不对马尾的,方法名对不上,参数有问题啊什么

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值