-1

My question is how to make a deep copy in java. Right now this is my code but I don't think this is correct.

@Override
public ListInterface<E> copy() {
    ListerInterface<E> temp = new List<E>();

    if (isEmpty()) {
        return null;
    } else {
        goToFirst();

        do {
            temp.inset(retrieve());
        } while (currentNode.next != null);

        currentNode = currentNode.next;
    }

    return temp;
}

So does anybody know what I should change in my code to get a deep copy that is correct?

Zabuzard
  • 25,064
  • 8
  • 58
  • 82
  • Please add a [Minimal, Complete, and Verifiable](https://stackoverflow.com/help/mcve) example. Since it is your first question on SO, you might read [the guide on asking good questions](https://stackoverflow.com/help/how-to-ask). – M. le Rutte Oct 14 '17 at 10:43
  • About the objects in references: do you need them to get cloned? – gthanop Oct 14 '17 at 10:47
  • Maybee consult [Arrays.deepEquals(...)](https://docs.oracle.com/javase/9/docs/api/java/util/Arrays.html#deepEquals-java.lang.Object:A-java.lang.Object:A-) method? – gthanop Oct 14 '17 at 10:48
  • 1
    Possible duplicate of [How do you make a deep copy of an object in Java?](https://stackoverflow.com/questions/64036/how-do-you-make-a-deep-copy-of-an-object-in-java). – Zabuzard Oct 14 '17 at 11:06

2 Answers2

1

Just use MicroStream Object Copier.

    ObjectCopier objectCopier = ObjectCopier.New();

    Customer customer = root.getCustomer(id);

    Customer customerCopy = objectCopier.copy(customer);

This utility provides the full deep copy of any object graph in Java. Be careful of the cyclic references. You can easily make o copy of your whole memory graph.

https://docs.microstream.one/manual/storage/storing-data/deep-copy.html

Zdenek Jonas
  • 121
  • 7
0

The code below copies a list which may contain other lists as referenced objects:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Main {
    public static void main(final String[] args) {
        final ArrayList list = new ArrayList();
        list.add(Arrays.asList("A", "B", "C"));
        list.add("D");
        System.out.println(list);
        final ArrayList out = new ArrayList();
        deepCopy(out, list);
        System.out.println(out);
    }

    public static void deepCopy(final List out,
                                final List in) {
        for (Object o: in)
            if (o instanceof List) {
                final ArrayList copy = new ArrayList();
                deepCopy(copy, (List)o);
                out.add(copy);
            }
            else
                out.add(o);
    }
}

Edit:
If you want the referenced objects to be cloned, you have to have access to the clone method of the referenced objects.

gthanop
  • 3,035
  • 2
  • 10
  • 27
  • Thank you and yes indeed I need to get the whole list cloned. – F.Verbeek Oct 14 '17 at 11:34
  • Do you need the referenced objects to be cloned? If yes, then do you need generic references or specific? Because if you go generic, then you have to have access to the clone method. – gthanop Oct 14 '17 at 11:53
  • I need a generic reference and I just found out that I forgot the clone method in my program – F.Verbeek Oct 14 '17 at 12:07