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

时间:2017-03-27 03:08:18

标签: java dozer

我有两个班级:

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列表,因为它们具有不同的字段名称但功能相同。请帮助我进行推土机映射,或者如果有其他解决方案,请高度赞赏。

谢谢!

1 个答案:

答案 0 :(得分:0)

尝试这样的事情:

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;
}