Linked Questions
86 questions linked to/from How do you make a deep copy of an object?
50 votes
7 answers
224k views
How to copy an object by value, not by reference [duplicate]
I want to make a copy of an object, then after some logic, re-assign the original object the value of the copy. example: User userCopy = //make a copy foreach(...) { user.Age = 1; user.ID = -1; ...
3 votes
2 answers
3k views
I have two lists containing the same objects. How do I change one list without changing the other? [duplicate]
I first noticed this problem when I only put the objects in listOfRates and then created inverseListOfRates by copying it. But even using this method, I can't alter one list without altering the other....
4 votes
3 answers
997 views
Java Copy Constructor with the "this" keyword [duplicate]
I have a pretty general question about java. I want to know if there is a simple way to recreate this c++ code in java: class A { public: int first; int second; A(const A& other) { *...
1 vote
2 answers
2k views
Create an independent copy of an array [duplicate]
I have an array and i want to create an independent copy of it. Changes in one of those should not impact the other at all. Currently everything i tried just created two references on one object. ...
1 vote
2 answers
1k views
How to deep copy an object in java. The object may or may not be serializable [duplicate]
How can we copy an object in java without copying the reference. If any object is present inside then we should copy the value only with out modifying the original object reference. How can this be ...
0 votes
1 answer
963 views
How to loop through a Map and clone each value? [duplicate]
I need to know how to for loop through a Hashtable, check for cloneability on each value, and clone if possible. This Hashtable has all String keys, but the values can be of any class. I have come ...
0 votes
0 answers
129 views
java - how to do deep copy of hashmap [duplicate]
How to deep copy hashmap? I wanna make a copy of hashmap. When I change the keys and values in the copy, the original hashmap doesn't change. I cannot find a solution to solve this problem in the ...
1 vote
1 answer
81 views
Problem with copying a single item of an ArrayList [duplicate]
I'm new at Java and I came across a problem I couldn't find a solution to, so maybe somebody can help me out. When I write this code: ArrayList<String> Liste = new ArrayList<>(); String ...
-3 votes
1 answer
94 views
Take a java object and store a copy somewhere else [duplicate]
I wanted to have a function that returns a copy of the object it receives, to use it somewhere else. This is what I wrote: public static PObject Instantiate(PObject obj) { PObject other = obj; ...
311 votes
21 answers
507k views
How to clone ArrayList and also clone its contents?
How can I clone an ArrayList and also clone its items in Java? For example I have: ArrayList<Dog> dogs = getDogs(); ArrayList<Dog> clonedList = ....something to do with dogs.... And I ...
200 votes
11 answers
171k views
Recommended solution for deep cloning/copying an instance [closed]
I'm wondering if there is a recommended way of doing deep clone/copy of instance in java. I have 3 solutions in mind, but I can have miss some, and I'd like to have your opinion edit: include Bohzo ...
98 votes
13 answers
301k views
How do I copy the contents of one ArrayList into another?
I have some data structures, and I would like to use one as a temporary, and another as not temporary. ArrayList<Object> myObject = new ArrayList<Object>(); ArrayList<Object> ...
80 votes
8 answers
95k views
Deep clone utility recommendation [closed]
Is there any utility for deep cloning for java collections: Arrays Lists Maps NOTE: prefer some solution without usage of serialization, but with use of Object.clone() method. I can be sure that my ...
24 votes
7 answers
15k views
Why is assignment to 'this' not allowed in java?
The error I get from the compiler is "The left hand side of an assignment must be a variable". My use case is deep copying, but is not really relevant. In C++, one can assign to *this. The ...
5 votes
7 answers
12k views
How to deep copy an irregular 2D array
How can I deep copy an irregularly shaped 2D array in Java? Ie. int[][] nums = {{5}, {9,4}, {1,7,8}, {8,3,2,10}} I'm unable to use Arrays....