BeanUtils. copy Properties 는 집합 을 복사 하 는 솔 루 션 을 지원 하지 않 습 니 다.
11448 단어 구덩이.
@Data
public class Class {
private People[] member;
private People teacher;
private List<People> student;
}
@Data
@NoArgsConstructor
@AllArgsConstructor
public class People {
private Integer id;
private String name;
private Integer age;
private Integer sex;
}
public static void main(String[] args) {
//
People[] member = new People[3];
member[0] = new People(1, " ", 30, 1);
member[1] = new People(2, " ", 15, 1);
member[2] = new People(3, " ", 15, 1);
People[] member1 = new People[]{};
BeanUtils.copyProperties(member, member1);
System.out.println(" :" + (member1.length == 0 ? false : true));
// List (Map , )
List student = new ArrayList<>();
student.add(member[1]);
student.add(member[2]);
List student1 = new ArrayList<>();
BeanUtils.copyProperties(student, student1);
System.out.println("BeanUtils.copyProperties List:" + (student1.isEmpty() ? false : true));
// JSON List ( List, Map , , )
student1 = JSON.parseArray(JSON.toJSONString(student), People.class);
System.out.println(" JSON List:" + student1);
System.out.println(" JSON :" + (student.get(0) != student1.get(0) ? true : false));
//
Class source = new Class();
source.setMember(member);
source.setTeacher(member[0]);
source.setStudent(student);
Class target = new Class();
BeanUtils.copyProperties(source, target);
System.out.println("BeanUtils.copyProperties :" + (source.getMember() != target.getMember() ? true : false));
}
:false
BeanUtils.copyProperties List:false
JSON List:[People(id=2, name= , age=15, sex=1), People(id=3, name= , age=15, sex=1)]
JSON :true
BeanUtils.copyProperties :false
List 에 대한 복 제 는 JSON 도 구 를 통 해 하 는 것 을 제외 하고 가장 간단 한 것 은 집합 속성 을 순환 적 으로 복제 하 는 것 이다. 다음은 두 가지 방법의 효율 을 측정 한다.
public static void main(String[] args) {
int count = 1;
System.out.println(" :" + count);
List<People> source = new LinkedList<>();
List<People> target = new LinkedList<>();
long start;
for (int i = 0; i < count; i++) {
source.add(new People(1, "ly", 25, 1));
}
start = System.nanoTime();
target = JSON.parseArray(JSON.toJSONString(source), People.class);
System.out.println("JSON:" + (System.nanoTime() - start));
start = System.nanoTime();
for (int i = 0; i < count; i++) {
People p = new People();
BeanUtils.copyProperties(source.get(i), p);
target.add(p);
}
System.out.println("BeanUtils.copyProperties" + (System.nanoTime() - start));
}
count = 1, 10, 100, 1000, 10000, 100000 의 결 과 를 각각 테스트 합 니 다.함께 실행 하 는 데 영향 을 미 치 는 것 을 방지 하기 위해 매번 한 가지 복제 방법의 한 가지 상황 만 테스트 하고 총 12 회 집행 한다.비 교 를 통 해 JSON 을 통 해 BeanUtils 보다 속성 이 빠 르 고,
:1
JSON:154767336
Bean:275182853
:10
JSON:165678435
Bean:275301421
:100
JSON:167937206
Bean:328461161
:1000
JSON:187832969
Bean:315815289
:10000
JSON:297461312
Bean:362763360
:100000
JSON:562035707
Bean:5815319343
복사 List, Map 을 다음 과 같이 해결 합 니 다.
public static List copyList(List list) {
if (CollectionUtils.isEmpty(list)) {
return new ArrayList();
}
return JSON.parseArray(JSON.toJSONString(list), list.get(0).getClass());
}
public static Map copyMap(Map map) {
return JSON.parseObject(JSON.toJSONString(map));
}