mirror of
https://gitee.com/dromara/hutool.git
synced 2025-08-20 09:28:59 +08:00
新增集合复制工具类,可自定义字段拷贝规则
This commit is contained in:
parent
b580030b5b
commit
5bb60a463d
@ -0,0 +1,50 @@
|
||||
package cn.hutool.core.bean;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
*
|
||||
* <p>
|
||||
* 集合复制工具类
|
||||
* <p>
|
||||
*
|
||||
* @author ms <br>
|
||||
* @email <br>
|
||||
* @date 2021-8-25 16:37:46 <br>
|
||||
*/
|
||||
public class BeanListCopyUtil extends BeanUtil {
|
||||
|
||||
/**
|
||||
* 集合数据的拷贝
|
||||
* @param sources: 数据源类
|
||||
* @param target: 目标类::new(eg: UserVO::new)
|
||||
* @return 复制后的集合数据
|
||||
*/
|
||||
public static <S, T> List<T> copyListProperties(List<S> sources, Supplier<T> target) {
|
||||
return copyListProperties(sources, target, null);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 带回调函数的集合数据的拷贝(可自定义字段拷贝规则)
|
||||
* @param sources: 数据源类
|
||||
* @param target: 目标类::new(eg: UserVO::new)
|
||||
* @param callBack: 回调函数
|
||||
* @return 复制后的集合数据
|
||||
*/
|
||||
public static <S, T> List<T> copyListProperties(List<S> sources, Supplier<T> target, BeanListCopyUtilCallBack<S, T> callBack) {
|
||||
List<T> list = new ArrayList<>(sources.size());
|
||||
for (S source : sources) {
|
||||
T t = target.get();
|
||||
copyProperties(source, t);
|
||||
list.add(t);
|
||||
if (callBack != null) {
|
||||
// 回调
|
||||
callBack.callBack(source, t);
|
||||
}
|
||||
}
|
||||
return list;
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,22 @@
|
||||
package cn.hutool.core.bean;
|
||||
|
||||
/**
|
||||
*
|
||||
* <p>
|
||||
* 集合复制回调工具类
|
||||
* <p>
|
||||
*
|
||||
* @author ms <br>
|
||||
* @email <br>
|
||||
* @date 2021-8-25 16:37:46 <br>
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface BeanListCopyUtilCallBack<S, T> {
|
||||
|
||||
/**
|
||||
* 定义默认回调方法
|
||||
* @param t
|
||||
* @param s
|
||||
*/
|
||||
void callBack(S t, T s);
|
||||
}
|
||||
@ -0,0 +1,60 @@
|
||||
package cn.hutool.core.bean;
|
||||
|
||||
import cn.hutool.core.lang.Console;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 集合复制工具类单元测试
|
||||
*
|
||||
* @author ms
|
||||
*/
|
||||
public class BeanListCopyUtilTest {
|
||||
|
||||
@Test
|
||||
public void copyListPropertiesTest() {
|
||||
List<TestEntity1> list1 = new ArrayList<>();
|
||||
TestEntity1 test1 = new TestEntity1();
|
||||
test1.setName("张三");
|
||||
test1.setSex(0);
|
||||
list1.add(test1);
|
||||
|
||||
TestEntity1 test2 = new TestEntity1();
|
||||
test2.setName("李四");
|
||||
test2.setSex(1);
|
||||
list1.add(test2);
|
||||
|
||||
List<TestEntity2> list2 = BeanListCopyUtil.copyListProperties(list1, TestEntity2::new);
|
||||
List<TestEntity2> list3 = BeanListCopyUtil.copyListProperties(list1, TestEntity2::new,
|
||||
new BeanListCopyUtilCallBack<TestEntity1,TestEntity2>() {
|
||||
@Override
|
||||
public void callBack(TestEntity1 source,TestEntity2 target) {
|
||||
target.setSexName(source.getSex() == 0 ? "男" : "女");
|
||||
}
|
||||
});
|
||||
Console.log(list1);
|
||||
Console.log(list2);
|
||||
Console.log(list3);
|
||||
|
||||
}
|
||||
|
||||
@Data
|
||||
@EqualsAndHashCode
|
||||
private static class TestEntity1 {
|
||||
private String name;
|
||||
private Integer sex;
|
||||
}
|
||||
|
||||
@Data
|
||||
@EqualsAndHashCode
|
||||
private static class TestEntity2 {
|
||||
private String name;
|
||||
private Integer sex;
|
||||
private String sexName;
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user