0

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
  • 2
    Why do you duplicate the code of class `test`? You can have multiple instance of the same class. That's object oriented programming. By the way, you should follow the Java coding conventions (class name should start with a capital letter). – LaurentG Dec 31 '13 at 06:18

2 Answers2

0

First thing - in your code you are skipping the root node (first name 1 and last name 1). Is that intentional? I'm assuming that's a bug, so I will fix it in the example below.

To do the deep copy, instead of recursively printing the values you would recursively build the tree by doing something like

private void deepCopyTree(Node node, Node nodeCopy) {
     if(node == null) { return; }
     nodeCopy.setFirstName(node.getFirstName());
     nodeCopy.setLastName(node.getLastName());

     if (node.getTestList() == null) { return; }

     for (Node child : node.getChildren()) { 
         Node copiedChild = new Node();
         nodeCopy.addChild(copiedChild);
         buildTree(child, copiedChild);
     }
}
Jack
  • 370
  • 2
  • 4
0

A pretty simple solution is to use a copy constructor. This will work because copying the list calls the copy constructor again on the children. The children will in turn copy their children. This solution is pretty generic.

class Test {
    List<Test> children;

    Test(Test other) {
        if(other.children != null) {
            children = new ArrayList<Test>(other.children.size());

            for(Test child : other.children)
                children.add(new Test(child)); // call copy constructor again
        }
    }
}

I don't understand the significance of your finalObject. It looks identical to test.

Radiodef
  • 37,180
  • 14
  • 90
  • 125