• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 279
  • Last Modified:

Cloning objects

Hi,

when I check how to work the Clone in http://www.cafeaulait.org/course/week4/46.html,
it explains that clones are doing shallow copy. If I need deep copy, I will need to override
clone() myself.

In this case, if  I want to perform deep copy for a Map<String, AObj>, do I need to go all the way down into the fields of AObj?

By the way, when I do:
String b = "abc"
String a = b,

is it doing deep copy or shallow copy?
0
amai
Asked:
amai
  • 3
  • 2
  • 2
  • +1
2 Solutions
 
Mayank SAssociate Director - Product EngineeringCommented:
>> is it doing deep copy or shallow copy?

Its a shallow copy because you are assigning 'a' to refer to the same String object "abc" to which 'b' refers. So you have only one String object (not 2) but 2 references to it.
0
 
Mayank SAssociate Director - Product EngineeringCommented:
The copyMap () method of this utility from Apache provides a deep copy of a Map:

http://jakarta.apache.org/commons/validator/api-1.2.0/org/apache/commons/validator/util/ValidatorUtils.html
0
 
amaiAuthor Commented:
I read about this method, and it specifies it will do deep copy if values are Msg, Arg, or Var. Otherwise, it is a shallow copy.

So, this will not serve the purpose if I'm copying a class that I've created rite?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
hoomanvCommented:
0
 
hoomanvCommented:
an alternative to deep copy is to use object serialization
http://www.javaworld.com/javaworld/javatips/jw-javatip76.html
0
 
Mayank SAssociate Director - Product EngineeringCommented:
Yes, serialization is an easy way to do it - just make sure all objects are serializable.
0
 
seet82Commented:
hi,it's me again
to deep copy an object, you will need to deepcopy all the instance of objects contained in the object

say you have
public class A
{
    public String string;
}
public class B
    public A a;
}
to deepcopy an instance of B, b
you will need to deepcopy the instance of a inside b

to deepcopy the instance of a, you will need to deep copy the instance of the string

offcause, for your case of Map<String, AObj>
things gets a bit messier but generally, for a map, i assume that the only important information is the String/AObj mapping.
thus, you will need to deepcopy the key and value and add to the deepcopied Map the deepcopied key/value

the below code should deepcopy the map

Map<String,AObj> mapA,mapB;
Map.Entry<String,AObj> mapEntry;
String string;
AObj aobj;
Collection collection = mapA.values();
Iterator<String,AObj> iterator = collection.iterator();
while (iterator.hasNext())
{
    mapEntry=iterator.next();
    string = new String(mapEntry.getKey());
    aobj = AObj(mapEntry.getValue()); // assume that the copy constructor of AObj deepcopies the AObj (which is a good practice to do so)
    mapB.put(string,aobj);
}    

the above code should deepcopy the map (minus any syntax error)
0
 
seet82Commented:
there's a typo mistake above (more like i forget to write some words)
 aobj = AObj(mapEntry.getValue()); // assume that the copy constructor of AObj deepcopies the AObj (which is a good practice to do so)

should be

 aobj = new AObj(mapEntry.getValue()); // assume that the copy constructor of AObj deepcopies the AObj (which is a good practice to do so)

ie, insert 'new'
0
 
amaiAuthor Commented:
Hi,

so I understand now. For all my constructor, I always prepare one that deep copy the object itself.

I will work on this. Thanks!
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now