java 数据copy工具包括list数据

该博客介绍了一个Java工具类`BeanConvertUtils`,它扩展了`BeanUtils`,提供了对象转换的功能。类中包含静态方法用于单个对象和对象列表的转换,并支持转换回调接口以自定义转换逻辑。示例代码展示了如何使用该工具类进行对象间的属性拷贝和自定义处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package org.jeecg.modules.system.util;

import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Supplier;

/**
 * 转换对象工具
 *
 * @author bugpool
 */
public class BeanConvertUtils extends BeanUtils {

    public static <S, T> T convertTo(S source, Supplier<T> targetSupplier) {
        return convertTo(source, targetSupplier, null);
    }

    /**
     * 转换对象
     *
     * @param source         源对象
     * @param targetSupplier 目标对象供应方
     * @param callBack       回调方法
     * @param <S>            源对象类型
     * @param <T>            目标对象类型
     * @return 目标对象
     */
    public static <S, T> T convertTo(S source, Supplier<T> targetSupplier, ConvertCallBack<S, T> callBack) {
        if (null == source || null == targetSupplier) {
            return null;
        }

        T target = targetSupplier.get();
        copyProperties(source, target);
        if (callBack != null) {
            callBack.callBack(source, target);
        }
        return target;
    }

    public static <S, T> List<T> convertListTo(List<S> sources, Supplier<T> targetSupplier) {
        return convertListTo(sources, targetSupplier, null);
    }

    /**
     * 转换对象
     *
     * @param sources        源对象list
     * @param targetSupplier 目标对象供应方
     * @param callBack       回调方法
     * @param <S>            源对象类型
     * @param <T>            目标对象类型
     * @return 目标对象list
     */
    public static <S, T> List<T> convertListTo(List<S> sources, Supplier<T> targetSupplier, ConvertCallBack<S, T> callBack) {
        if (null == sources || null == targetSupplier) {
            return null;
        }

        List<T> list = new ArrayList<>(sources.size());
        for (S source : sources) {
            T target = targetSupplier.get();
            copyProperties(source, target);
            if (callBack != null) {
                callBack.callBack(source, target);
            }
            list.add(target);
        }
        return list;
    }

    /**
     * 回调接口
     *
     * @param <S> 源对象类型
     * @param <T> 目标对象类型
     */
    @FunctionalInterface
    public interface ConvertCallBack<S, T> {
        void callBack(S t, T s);
    }




    public static void main(String[] args) {
     /* Student student = new Student();
      student.setName("1q21");
      student.setSex("11");
      student.setAddress("啊手动阀");
        StudentVo studentVo = BeanConvertUtils.convertTo(student, StudentVo::new,(s, t) -> t.setName(s.getAddress()));
        System.out.println(student);
        System.out.println(studentVo);*/

/*
        Student student = new Student();
        student.setName("1q21");
        student.setSex("11");
        student.setAddress("啊手动阀");

        Student student2 = new Student();
        student2.setName("2");
        student2.setSex("22");
        student2.setAddress("222");
        List<Student> studentList = new ArrayList<>();
        studentList.add(student);
        studentList.add(student2);
        List<StudentVo> studentVos = BeanConvertUtils.convertListTo(studentList, StudentVo::new,(s, t) -> t.setAddress(s.getName()));
        System.out.println(studentVos);
*/

    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值