How can I clone an ArrayList
and also clone its items in Java?
For example I have:
ArrayList dogs = getDogs();
ArrayList
I have found a way, you can use json to serialize/unserialize the list. The serialized list holds no reference to the original object when unserialized.
Using gson:
List originalList = new ArrayList<>(); // add some items later
String listAsJson = gson.toJson(originalList);
List newList = new Gson().fromJson(listAsJson, new TypeToken>() {}.getType());
You can do that using jackson and any other json library too.