简体   繁体   English

如何将值从一个Java类复制到具有相同属性的另一个类

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

相关问题 如何在Java中将数组列表从一个类复制到另一个类? - How do I copy an arraylist from one class to another in Java? Java-将静态arrayList从一个类复制到另一个类的HashMap - Java - copy static arrayList from one class to a HashMap in another class Java-将值从一类传递到另一类 - Java - Passing values from one class to another JAVA:将值从一类传递到另一类 - JAVA: Passing values from one class to another 将一个类属性复制到另一个类,并在 java Spring boot 中设置要映射的字段 - Copy one class properties to another class, with set the field to map in java Spring boot 如何从同一页面的另一个Java类中的一个Java类获取值/元素 - How to obtain a value/element from one Java class in another Java class in the same page 通过反射将一个 class 中字段的所有值复制到另一个 - Copy all values from fields in one class to another through reflection 如何将一个对象字段数据复制到同一类的另一对象? - How it copy one object fields data to another object of the same class? 如何将属性从一个Java bean复制到另一个? - How to copy properties from one Java bean to another? 将 java 对象/类从一个类加载器复制到另一个类加载器 - Copy java object/class from one classloader to another classloader
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM