0

I have a Node object and want to make some clone objects from this orginal one. Below is my code:

public class Main {
    public static void main(String[] args) {
        Node node = new Node();
        node.setName("node1");
        node.setValue("1");
        node.setChildNodes(new ArrayList<>());

        Node node2 = cloneNode(node);
        Node childNode2 = new Node();
        childNode2.setName("childNode2");
        childNode2.setValue("1");
        List<Node> childNodes2 = node2.getChildNodes();
        childNodes2.add(childNode2);
        node2.setChildNodes(childNodes2);
        System.out.println(node.getChildNodes());

    }

    private static Node cloneNode(Node node) {
        Node newNode = new Node();
        newNode.setName(node.getName());
        newNode.setValue(node.getValue());
        newNode.setChildNodes(node.getChildNodes());
        return newNode;
    }

}

class Node {
    private String name;
    private String value;
    private List<Node> childNodes;

    //getter and setter
}

I created a first Node and create a second Node which is cloned from the first one. Then I would like to modify the childNodes list in the second Node object (node2) and then I realize that the childNodes list in the first Node(node) is also modified.

How can I avoid the list in the first Node being modified?

Thank you so much!

Ahmed Salah
  • 851
  • 2
  • 10
  • 29
Joe
  • 21
  • 2
  • You clone the node, but you don't clone the children--it's the same list. – Dave Newton Mar 11 '19 at 14:52
  • 2
    You need to do a deep copy and clone the child nodes as well – Joakim Danielson Mar 11 '19 at 14:52
  • 1
    Show the code for `getChildNodes()` please. It's likely you need to copy the list however. – markspace Mar 11 '19 at 14:52
  • Something like `newNode.setChildNodes(new ArrayList(node.getChildNodes()));` See also: [shallow copy of linkedlist does not reflect changes when adding new node](https://stackoverflow.com/questions/55080507/shallow-copy-of-linkedlist-does-not-reflect-changes-when-adding-new-node) – Michał Ziober Mar 11 '19 at 14:53
  • Yes, "and also clone its contents" which is a list of nodes. You'll be copying forever. This might be bad design/XY problem wrt your list of nodes or the idea that you should clone a node. – markspace Mar 11 '19 at 14:54
  • create a new Node, iterate over the old nodes' values and clone them as well into the new one – aran Mar 11 '19 at 14:56
  • I'd ask if there's any chance that the node graph could be circular. Can a node "lower down" refer to a node that's higher up? Is this a tree or a more general graph? – markspace Mar 11 '19 at 14:57

1 Answers1

2

What you have done is a kind of shallow clone. You might want to clone deeper instead of

newNode.setChildNodes(node.getChildNodes())

You should clone the child nodes as well. Proper way is this

Alternatively , you can just serialize and deserialize the object is the easy way of cloning deep objects

Eg: Just transform the Node1 to JSON string and convert the string back to Node2 (Tip: use the gson library)

Viswanath Lekshmanan
  • 9,945
  • 1
  • 40
  • 64