java不同对象的集合拷贝_Java的对象的拷贝方式集合

1. 反射

使用了缺省的方式进行copy。要求源对象和目标对象必须实现getter和setter。进行copy时,内部会缓存缺省对象,以优化性能。

源码位置:org.springframework.beans.BeanUtils

public class TestJavaUtils {

public static void main(String[] args) {

UserPo userPo=new UserPo();

userPo.setId(1001L);

userPo.setName("tom");

userPo.setAge(12);

UserDto userDto=new UserDto();

//源对象:目标对象

BeanUtils.copyProperties(userPo,userDto);

System.out.println(userDto);

}

@Data

public static class UserDto{

private Long id;

private String name;

private Integer age;

private String sex;

}

@Data

public static class UserPo{

private Long id;

private String name;

private Integer age;

}

}

2. cglib字节码

copy对象时,可以使用CGLib的BeanCopier(其原理是运行时动态生成了用于复制某个类的字节码),其性能比反射框架org.springframework.beans.BeanUtils性能要高。

/**

* @param source 原始对象

* @param targetClass 拷贝的对象类型

*/

public static < T1,T2 > T2 createCopy(T1 source, Class < T2 > targetClass) {

if (source == null) {

throw new RuntimeException("参数异常");

} else {

T2 target;

try {

target = targetClass.newInstance();

} catch(Exception e) {

throw new RuntimeException(e);

}

BeanCopier beanCopier = BeanCopier.create(source.getClass(), targetClass, false);

beanCopier.copy(source, target, null);

return target;

}

}

3. get和set的链式拷贝

本质是使用getter和setter的方式进行copy,但是借助了jdk8的链式编程。

org.springframework.amqp.utils.JavaUtils源码位置(未引入rabbitMq可以粘贴代码到项目里面):

源码中使用如下方式进行copy的。

public final class JavaUtils {

/**

* The singleton instance of this utility class.

*/

public static final JavaUtils INSTANCE = new JavaUtils();

private JavaUtils() {

super();

}

/**

* Invoke {@link Consumer#accept(Object)} with the value if the condition is true.

* @param condition the condition.

* @param value the value.

* @param consumer the consumer.

* @param the value type.

* @return this.

*/

public JavaUtils acceptIfCondition(boolean condition, T value, Consumer consumer) {

if (condition) {

consumer.accept(value);

}

return this;

}

/**

* Invoke {@link Consumer#accept(Object)} with the value if it is not null.

* @param value the value.

* @param consumer the consumer.

* @param the value type.

* @return this.

*/

public JavaUtils acceptIfNotNull(T value, Consumer consumer) {

if (value != null) {

consumer.accept(value);

}

return this;

}

/**

* Invoke {@link Consumer#accept(Object)} with the value if it is not null or empty.

* @param value the value.

* @param consumer the consumer.

* @return this.

*/

public JavaUtils acceptIfHasText(String value, Consumer consumer) {

if (StringUtils.hasText(value)) {

consumer.accept(value);

}

return this;

}

/**

* Invoke {@link BiConsumer#accept(Object, Object)} with the arguments if the

* condition is true.

* @param condition the condition.

* @param t1 the first consumer argument

* @param t2 the second consumer argument

* @param consumer the consumer.

* @param the first argument type.

* @param the second argument type.

* @return this.

*/

public JavaUtils acceptIfCondition(boolean condition, T1 t1, T2 t2, BiConsumer consumer) {

if (condition) {

consumer.accept(t1, t2);

}

return this;

}

/**

* Invoke {@link BiConsumer#accept(Object, Object)} with the arguments if the t2

* argument is not null.

* @param t1 the first argument

* @param t2 the second consumer argument

* @param consumer the consumer.

* @param the first argument type.

* @param the second argument type.

* @return this.

*/

public JavaUtils acceptIfNotNull(T1 t1, T2 t2, BiConsumer consumer) {

if (t2 != null) {

consumer.accept(t1, t2);

}

return this;

}

/**

* Invoke {@link BiConsumer#accept(Object, Object)} with the arguments if the value

* argument is not null or empty.

* @param t1 the first consumer argument.

* @param value the second consumer argument

* @param the first argument type.

* @param consumer the consumer.

* @return this.

*/

public JavaUtils acceptIfHasText(T t1, String value, BiConsumer consumer) {

if (StringUtils.hasText(value)) {

consumer.accept(t1, value);

}

return this;

}

}

测试用例:

public class TestJavaUtils {

/**

* 对象的copy链式。

*/

public static void main(String[] args) {

UserPo userPo=new UserPo();

userPo.setId(1001L);

userPo.setName("tom");

userPo.setAge(12);

UserDto userDto=new UserDto();

JavaUtils.INSTANCE.acceptIfNotNull(userPo.getAge(),userDto::setAge).acceptIfNotNull(userPo.getName(),userDto::setName);

System.out.println(userDto);

}

@Data

public static class UserDto{

private Long id;

private String name;

private Integer age;

private String sex;

}

@Data

public static class UserPo{

private Long id;

private String name;

private Integer age;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值