[英]How to copy the values from one Java class to another class with same properties
Here is my class structure: (with omitted getters and setters) 这是我的课程结构:(省略了getter和setter)
public class A {
public List<QuestionTemplate> qTemplateList;
}
public class QuestionTemplate {
public List<QuestionList> qList;
}
public class QuestionList {
public String questionText;
public String questionChoice;
}
----------------------------------------------------
public class B {
public List<QuestionTemplate> qTemplateList;
}
public class QuestionTemplate {
public List<QuestionList> qList;
}
public class QuestionList {
public String questionText;
public String questionChoice;
}
I would like to copy the data from class B to class A by hand, manually copy fields instead of using any type of mapper. 我想手动将数据从B类复制到A类,手动复制字段,而不使用任何类型的映射器。
I tried walking the lists and tried to copy from one list to the other (starting with the most inner list but ran into tons of issues. Please forgive as I am new to this. I tried. Please help. 我尝试遍历列表,并尝试从一个列表复制到另一个列表(从最内部的列表开始,但遇到了很多问题。请原谅我,这是我的新手。我尝试过。请帮助。
Please refer this Copy all values from fields in one class to another through reflection 请参阅此内容,通过反射将所有值从一类的字段复制到另一类
Similar problem is already answered there. 那里已经回答了类似的问题。
This code requires Java 7 or later. 此代码需要Java 7或更高版本。
Option A Copy the data manually by iterating through: 选项A通过以下方式手动复制数据:
A a = new A();
..
List<QuestionTemplate> templateListCopy = new LinkedList<>();
for (QuestionTemplate template : a.qTemplateList) {
List<QuestionList> questionListCopy = new LinkedList<>();
for (QuestionList question : template.qList) {
QuestionList questionCopy = new QuestionList();
questionCopy.questionText = question.questionText;
questionCopy.questionChoice = question.questionChoice;
questionListCopy.add(questionCopy);
}
QuestionTemplate questionTemplateCopy = new QuestionTemplate();
questionTemplateCopy.qList = questionListCopy;
templateListCopy.add(questionTemplateCopy);
}
B b = new B();
b.qTemplateList = templateListCopy;
Option B Modify the classes and add copy methods to make the implementation code much less confusing: 选项B修改类并添加复制方法,以减少实现代码的混乱:
class A {
public List<QuestionTemplate> qTemplateList;
public A copy() {
A copy = new A();
List<QuestionTemplate> questionTemplateListCopy = new ArrayList<>(qTemplateList.size());
for (QuestionTemplate questionTemplate : qTemplateList) {
questionTemplateListCopy.add(questionTemplate.copy());
}
copy.qTemplateList = questionTemplateListCopy;
return copy;
}
}
class QuestionTemplate {
public List<QuestionList> qList;
public QuestionTemplate copy() {
QuestionTemplate copy = new QuestionTemplate();
List<QuestionList> qListCopy = new ArrayList<>(qList.size());
for (QuestionList questionList : qList) {
qListCopy.add(questionList.copy());
}
copy.qList = qListCopy;
return copy;
}
}
class QuestionList {
public String questionText;
public String questionChoice;
public QuestionList copy() {
QuestionList copy = new QuestionList();
copy.questionText = questionText;
copy.questionChoice = questionChoice;
return copy;
}
}
Implementation: 实现方式:
A a = new A();
..
B b = new B();
b.qTemplateList = a.copy().qTemplateList;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.