Results 1 to 6 of 6
Thread: [SOLVED] Deep copy
- 06-05-2009, 01:28 PM #1
Member
- Join Date
- Apr 2009
- Posts
- 12
- Rep Power
- 0
[SOLVED] Deep copy
Hi,
Im trying to clone a list of objects in my application.
this is the object hierarchy
Java Code:public class M implements Cloneable, java.io.Serializable { private static final long serialVersionUID = 1L; private List<S> s; private Map<String, Object> parameters;//getters included, sniped @Override public Object clone() { try { return super.clone(); } catch (CloneNotSupportedException ex) { System.out.println("Cloning of M failed"); throw new InternalError(ex.toString()); } } }
this class includes a list of this class
Java Code:public class S implements Cloneable, java.io.Serializable { private static final long serialVersionUID = 1L; private Map<String, Object> parameters; @Override public Object clone() { try { return super.clone(); } catch (CloneNotSupportedException ex) { System.out.println("Cloning of Sfailed"); throw new InternalError(ex.toString()); } } }
In my application i create an set of class M objects, each containing a list of S objects. This set only has default parameters. i need to be able to grab a copy of this set and modify it without effecting the original contents - this is why i implemented cloneable.
This is my attempt to do this :
Java Code:class ObjectDao{ private static ArrayList<M> allMs = new ArrayList<M>(); private void addM(M m){ allMs.add(m); } private static ArrayList<M> getClonedMList(){ ArrayList<M> clonedMs = new ArrayList<M>(); try { for( int i = 0;i < allMs.size();i++ ) { M clonedM = (M)allMs.get(i).clone(); clonedM.setParameters( (HashMap) ( (HashMap) allMs.get(i).getParameters() ).clone() ); for(int j = 0;j < ( allMs.get(i).getSs().size() );j++) { S clonedS= (S) allMs.get(i).getSs().get(j).clone(); clonedS.setParameters( (HashMap) ( (HashMap) allMs.get(j).getSs().get(i).getParameters() ).clone() ); clonedM.getSs().set(j, clonedS); } clonedMs.add(clonedM); } } catch (Exception e) { log.error(e.toString()); } return clonedMs; }
I clone each object with the parent object individually but it doesnt have the desired effect i.e. if i modify an S's parameters (on an M received from this method) somewhere else in the application the next call to this method returns objects with the modified parameters instead of the original state
Can anybody help?
- 06-05-2009, 02:06 PM #2
i think you have to read about how to ask questions gently?
Mak
(Living @ Virtual World)
- 06-05-2009, 02:10 PM #3
Your clone() methods have to work through your lists and maps and deep copy them too. super.clone() just performs a shallow copy of the the outer object.
Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
How To Ask Questions The Smart Way
- 06-05-2009, 02:25 PM #4
Member
- Join Date
- Apr 2009
- Posts
- 12
- Rep Power
- 0
Is serialising and then deserialising an object the only way of performing a deep copy?
This is simply too slow for what i need
- 06-05-2009, 03:44 PM #5
Not at all. Usually you just have to iterate through everything. Serialisation is for serialising things, not cloning them.
Note that Strings, and other immutable objects, don't need cloning.Java Code:public class Example implements Cloneable { private Cloneable[] array; Map<String,Cloneable> map; int primitive; Cloneable object; public Example clone() { Example clone = new Example(); clone.primitive = this.primitive; clone.object = this.object.clone(); clone.array = new Cloneable[this.array.length]; for (int i=0; i<array.length; i++) { clone.array[i] = this.array[i].clone(); } clone.map = new Map<String,Cloneable>(); for (String s : map.getKeys()) { clone.map.put(s, this.map.get(s).clone()); } return clone; } }Don't forget to mark threads as [SOLVED] and give reps to helpful posts.
How To Ask Questions The Smart Way
- 06-05-2009, 03:51 PM #6
Member
- Join Date
- Apr 2009
- Posts
- 12
- Rep Power
- 0
Similar Threads
-
Deep copy?!
By deepthought015 in forum New To JavaReplies: 3Last Post: 05-07-2009, 06:31 PM -
Help with Deep Cloning
By jralexander137 in forum New To JavaReplies: 6Last Post: 10-30-2008, 05:34 PM -
deep copying arraylist to add to a list
By alvations in forum New To JavaReplies: 13Last Post: 10-08-2008, 03:13 PM -
Deep Copy Test
By Java Tip in forum java.langReplies: 0Last Post: 04-16-2008, 11:05 PM -
how can i deep copy objects themselves instead of handles or references.. ?
By ishakteyran in forum Advanced JavaReplies: 0Last Post: 12-28-2007, 12:25 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks