栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java如何拷贝复制对象和集合

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java如何拷贝复制对象和集合

在Java中常见的对象拷贝方式是使用cglib,Cglib的性能是目前公认最好的,用于解决Bean拷贝的性能问题

下面是以cglib封装的工具类实现对象和集合的拷贝

一、引入依赖

     cglib
     cglib
     3.3.0
     compile
二、创建CglibUtil工具类

其中包含对象和集合的拷贝,还封装了Bean转Map的方法

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ReflectUtil;
import net.sf.cglib.beans.BeanCopier;
import net.sf.cglib.beans.BeanMap;
import net.sf.cglib.core.Converter;

import java.util.Collection;
import java.util.List;
import java.util.function.BiConsumer;
import java.util.function.Supplier;
import java.util.stream.Collectors;

public class CglibUtil {

    
    public static  T copy(Object source, Class targetClass) {
        return copy(source, targetClass, null);
    }

    
    public static  T copy(Object source, Class targetClass, Converter converter) {
        final T target = ReflectUtil.newInstanceIfPossible(targetClass);
        copy(source, target, converter);
        return target;
    }

    
    public static void copy(Object source, Object target) {
        copy(source, target, null);
    }

    
    public static void copy(Object source, Object target, Converter converter) {
        Assert.notNull(source, "Source bean must be not null.");
        Assert.notNull(target, "Target bean must be not null.");

        final Class sourceClass = source.getClass();
        final Class targetClass = target.getClass();
        boolean conIsNull = converter == null ? false : true;
        final BeanCopier beanCopier = BeanCopier.create(sourceClass, targetClass, conIsNull);
        beanCopier.copy(source, target, converter);
    }

    
    public static  List copyList(Collection source, Supplier target) {
        return copyList(source, target, null, null);
    }

    
    public static  List copyList(Collection source, Supplier target, Converter converter) {
        return copyList(source, target, converter, null);
    }

    
    public static  List copyList(Collection source, Supplier target, BiConsumer callback) {
        return copyList(source, target, null, callback);
    }

    
    public static  List copyList(Collection source, Supplier target, Converter converter, BiConsumer callback) {
        return source.stream().map(s -> {
            T t = target.get();
            copy(s, t, converter);
            if (callback != null) {
                callback.accept(s, t);
            }
            return t;
        }).collect(Collectors.toList());
    }

    
    public static BeanMap toMap(Object bean) {
        return BeanMap.create(bean);
    }
}
三、测试拷贝对象和集合

拷贝对象测试,再将拷贝的对象和源对象放入集合中对拷贝集合测试

 public static void main(String[] args) {
        
        //创建对象
        TUser tUser  = new TUser();
        tUser.setName("李四");
        //拷贝对象
        TUser tUser2 = CglibUtil.copy(tUser,TUser.class);
        //打印结果
        System.out.println(tUser2.getName());
        
        //创建集合
        List list1 = new ArrayList<>();
        list1.add(tUser);
        list1.add(tUser2);
        //拷贝集合
        List list2 = CglibUtil.copyList(list1,TUser::new);
        //打印结果
        System.out.println(JSONObject.toJSONString(list2));
    }

结果打印

四、注意点

CglibUtil.copy拷贝对象的时候,目标对象被lombok注解@Accessors(chain = true)修饰报空指针

在拷贝对象上不可用注解 @Accessors ,反射获取不到对应的setter,因为这个链式生成的代码带了返回值,此注解会将set方法返回值改变

转载请注明:文章转载自 www.mshxw.com
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号