I am asking this question through a sample program. I do have a usecase where i have a object tree/hierarchy. Imagine there is a pojo named test which has two fields firstname, lastname and List testList. Now i am creating a hierarchy of objects something like as shown below.
test obj1
|-- test obj2
| |-- test obj4
| |-- test obj5
|-- test obj3
test obj1 has a list of two objects named test obj2 and test obj3. Internally test obj2 has a list of two more objects named test obj4 and test obj5
In this printTreeContents method in the class test1 i am able to print all the attribute values of the test object hierarchy. Now I would like to populate the same test object hierarchy into another object named finalObject which also has similar attributes like test object. Basically i would like to replicate the same test object tree in to this new finalObject tree. Any idea how could i go ahead doing this? I would like to have a generic solution coz the hierarchy of objects will differ.
finalObject1
|-- finalObject2
| |-- finalObject4
| |-- finalObject5
|-- finalObject3
finalObject1 has a list of two objects named finalObject2 and finalObject3. Internally finalObject2 has a list of two more objects named finalObject4 and finalObject5
public class test {
private String firstName;
private String lastName;
private List<test> testList;
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getFirstName() {
return firstName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getLastName() {
return lastName;
}
public void setTestList(List<test> testList) {
this.testList = testList;
}
public List<test> getTestList() {
return testList;
}
}
public class test1 {
public static void main(String[] args) {
ArrayList<test> objList = new ArrayList<test>();
test obj = new test();
obj.setFirstName("firstname1");
obj.setLastName("lastname1");
test obj2 = new test();
obj2.setFirstName("firstname2");
obj2.setLastName("lastname2");
test obj3 = new test();
obj3.setFirstName("firstname3");
obj3.setLastName("lastname3");
test obj4 = new test();
obj4.setFirstName("firstname4");
obj4.setLastName("lastname4");
test obj5 = new test();
obj5.setFirstName("firstname5");
obj5.setLastName("lastname5");
ArrayList<test> childobjList2 = new ArrayList<test>();
childobjList2.add(obj4);
childobjList2.add(obj5);
obj2.setTestList(childobjList2);
ArrayList<test> childobjList = new ArrayList<test>();
childobjList.add(obj2);
childobjList.add(obj3);
obj.setTestList(childobjList);
objList.add(obj);
System.out.println(" Object Tree creation Completed");
test1 tst1 = new test1();
tst1.printTreeContents(obj);
System.out.println("Iterating through object tree completed");
}
private void printTreeContents(test node) {
if (node.getTestList() != null)
{
for (test child : node.getTestList())
{
System.out.println("first name is " + child.getFirstName());
System.out.println("last name is " + child.getLastName());
printTreeContents(child);
}
}
}
}
public class finalObject{
private String firstName;
private String lastName;
private List<test> testList;
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getFirstName() {
return firstName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getLastName() {
return lastName;
}
public void setTestList(List<test> testList) {
this.testList = testList;
}
public List<test> getTestList() {
return testList;
}
}
Output of the above program is
Object Tree creation Completed
first name is firstname2
last name is lastname2
first name is firstname4
last name is lastname4
first name is firstname5
last name is lastname5
first name is firstname3
last name is lastname3
Iterating through object tree completed