簡體   English   中英

Java - 映射包含對象列表的兩個對象(具有不同命名但功能相同的對象)

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM