0

Here is my class structure: (with omitted getters and setters)

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.

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.

hln98
  • 155
  • 2
  • 11
  • It is always better for you to show your best good-faith attempt at a solution in your question, no matter how bad, and tell the specific problems that you have faced with it. – Hovercraft Full Of Eels Jul 29 '19 at 01:26
  • Do you just want to copy the "references" or do you literally want to create new instances of the objects? – MadProgrammer Jul 29 '19 at 01:27
  • What makes the classes complex? When you say by hand do you mean without using any built in methods in the List interface or Stream class? – CausingUnderflowsEverywhere Jul 29 '19 at 01:53
  • You shouldn't define the QuestionTemplate and QuestionList classes twice. Just put them in separate files and that will do. Class A and Class B also look the same, you can just rename class A to QuestionTemplateList and get rid of class B. You can use a and b as instance names of this class. Eg. QuestionTemplateList a = new QuestionTemplateList(); QuestionTemplateList b = new QuestionTemplateList(); then iterate the question templates in instance a and generate a new question template list copy to put inside b. – CausingUnderflowsEverywhere Jul 29 '19 at 01:59

2 Answers2

0

Please refer this Copy all values from fields in one class to another through reflection

Similar problem is already answered there.

Pradeep
  • 350
  • 1
  • 3
  • 13
0

This code requires Java 7 or later.

Option A Copy the data manually by iterating through:

    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:

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;