简体   繁体   中英

Java - Map two objects containing List of objects(objects which have different naming but same function)

I have two classes:

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

I have to map/copy values from class1 to class2. I tried to use dozer bean mapper API, but I could not map List of Student with List of Person as they have different field names but same function. Please help me with dozer mapping or if there is other solution, highly appreciated.

Thanks!

Try something like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM