[英]Java - Map two objects containing List of objects(objects which have different naming but same function)
我有两节课:
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 mapper API,但是我无法使用Person列表映射Student列表,因为它们具有不同的字段名称但功能相同。 请帮助我做推土机映射或如果有其他解决方案,高度赞赏。
谢谢!
尝试这样的事情:
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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.