浅封装BeanUtils,优雅实现List数据copy拷贝

创建BeanConvertUtils

import java.util.ArrayList;
import java.util.List;
import java.util.function.Supplier;
import org.springframework.beans.BeanUtils;

/**
 * 转换对象工具
 * @author weimeilayer@gmail.com ✨
 * @date 💓💕 2024年6月18日 🐬🐇 💓💕
 */
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);
    }
}

使用方法

	// 创建源对象列表
   List<SourceEntity> sourceList = new ArrayList<>();
    sourceList.add(new SourceEntity(1L, "John"));
    sourceList.add(new SourceEntity(2L, "Alice"));
    sourceList.add(new SourceEntity(3L, "Bob"));
	 // 定义目标对象的供应方(Supplier)
     Supplier<TargetDTO> targetSupplier = TargetDTO::new;
      // 转换对象列表
      List<TargetDTO> targetList = BeanConvertUtils.convertListTo(sourceList, targetSupplier, (source, target) -> {
          // 可以在回调函数中进行额外的处理
          // 比如特定字段的赋值等
          target.setId(source.getId()); // 示例:将源对象的 id 赋值给目标对象的 id
      });
      // 输出转换后的目标对象列表
      for (TargetDTO dto : targetList) {
          System.out.println("ID: " + dto.getId() + ", Name: " + dto.getName());
      }

第二种BeanUtils方法

import java.lang.reflect.InvocationTargetException;
import org.springframework.beans.BeanUtils;
import java.util.ArrayList;
import java.util.List;

/**
 * @author weimeilayer@gmail.com ✨
 * @date 💓💕 2024年6月18日 🐬🐇 💓💕
 */
public class BeanUtilsHelper {

    /**
     * 将源列表中的数据复制到目标列表中,并返回目标列表。
     *
     * @param sourceList 源列表
     * @param targetClass 目标列表中元素的类
     * @return 目标列表
     */
    public static <S, T> List<T> copyList(List<S> sourceList, Class<T> targetClass) {
        List<T> targetList = new ArrayList<>();
        for (S source : sourceList) {
            T target;
            try {
                target = targetClass.getDeclaredConstructor().newInstance();
                BeanUtils.copyProperties(target, source);
                targetList.add(target);
            } catch (InstantiationException | IllegalAccessException | InvocationTargetException
                    | NoSuchMethodException e) {
                // 处理异常
                e.printStackTrace();
            }
        }
        return targetList;
    }
}

List<SourceClass> sourceList = ...; // 假设这里集合列表数据
List<TargetClass> targetList = BeanUtilsHelper.copyList(sourceList, TargetClass.class);

相关推荐

  1. 封装BeanUtils优雅实现List数据copy拷贝

    2024-06-19 04:06:02       7 阅读
  2. 改造BeanUtils优雅实现List数据拷贝

    2024-06-19 04:06:02       12 阅读
  3. np.copy()是深拷贝还是拷贝

    2024-06-19 04:06:02       40 阅读
  4. BeanUtil.copyProperties(source,target)拷贝List注意事项

    2024-06-19 04:06:02       38 阅读
  5. 拷贝与深拷贝全面解析及实战

    2024-06-19 04:06:02       6 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-06-19 04:06:02       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-19 04:06:02       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-19 04:06:02       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-19 04:06:02       18 阅读

热门阅读

  1. 8086/8088计算机寄存器知识详解

    2024-06-19 04:06:02       10 阅读
  2. [qt][报错】[Makefile:1293: moc_widget.cpp] Error 1

    2024-06-19 04:06:02       7 阅读
  3. 【Qt】xml文件节点读取

    2024-06-19 04:06:02       5 阅读
  4. 6、while循环 - 习题解析

    2024-06-19 04:06:02       5 阅读
  5. 华为OD机试 C++ - 跳格子1

    2024-06-19 04:06:02       8 阅读
  6. LeetCode 14. 最长公共前缀

    2024-06-19 04:06:02       6 阅读
  7. 部署YUM仓库及NFS共享服务

    2024-06-19 04:06:02       5 阅读