gpt4 book ai didi

Java - 映射包含对象列表的两个对象(具有不同命名但功能相同的对象)

转载 作者:太空宇宙 更新时间:2023-11-04 11:38:23 26 4
gpt4 key购买 nike

我有两门课:

public class1{
private int id;
private List<Student> students;
}

public Student{
private name;
private address;
}

public Class2{
private int id;
private List<Person> person;
}

public Person{
private personName;
private location;
}

我必须将值从 class1 映射/复制到 class2。我尝试使用 dozer bean 映射器 API,但无法将学生列表与人员列表映射,因为它们具有不同的字段名称但具有相同的功能。请帮助我进行推土机测绘,或者如果有其他解决方案,非常感谢。

谢谢!

最佳答案

尝试这样的事情:

public static List<Person> mapValues() {

List<Student> students = class1.getStudents(); // Assuming you have getters of students field
List<Person> persons = class2.getPersons(); // Assuming you have getters of persons field
for(Student student: students) {
Person person = new Person();
person.setPersonName(student.getName);
person.setLocation(student.getAddress);
persons.add(person);
}
return persons;
}

关于Java - 映射包含对象列表的两个对象(具有不同命名但功能相同的对象),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43037331/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com