Deep Copy Performance

hai guys..
iam using this method given below for deepycopy .....is there any other method which can improve me the performance of deepcopying......i need it since my logic uses deepcopying many times
public static Object copy(Object orig) {
Object obj = null;
try {
// Write the object out to a byte array
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(bos);
out.writeObject(orig);
out.flush();
out.close();
// Make an input stream from the byte array and read
// a copy of the object back in.
ObjectInputStream in = new ObjectInputStream(
new ByteArrayInputStream(bos.toByteArray()));
obj = in.readObject();
catch(IOException e) {
e.printStackTrace();
catch(ClassNotFoundException cnfe) {
cnfe.printStackTrace();
return obj;
}

Hi,
I am using the same method than you to perform the DeepCopy.
Of course that implementing cloneable on each of the fields used in the class and then implement clone in the main parent class like this:
public MyClass clone(){
          MyClass newclass = new MyClass();
          newclass.setField( this.getField().clone() ) ;
          return newclass;
}kind of thing....
The problem is that if you have a complicated class, it is much easier to implement Serializable in the classes that you are going to use and try your luck.
Is there an Eclipse shortcut to creting clone methods??
That would be quite nice I think, cos it is really tedious to do by hand.
Cheers,
Alf

Similar Messages

  • Clarification regarding deep cloning/deep copy

    Hi,
    While performing deep copy of an object should all the subobject that the parent object constitute also constitute the clone method.
    For Eg:
    I have a class called Document and a class called Field. Document constitutes of List<Field> fields. Field class has a String fieldName as its member variable.
    I need to perform a deep copy of Document object. Should Field class also implement the clone method or can I create field objects in the documents clone method and fill these fields with values from the existing field list to form a new Document with these new fields.
    Or do I call clone() method on each field to get a new field object and
    add it to the new Document object that I am creating in the clone() method .
    //First approach
    public Document clone(){
    Document doc = new Document();
    List<Field> fields = doc.getFields()
    Field newField = null;
    for(Field f: fields){
        newField = new Field();
        newField.setName(f.getName());
       doc.addField(newField);
    return doc;
    //Second Approach
    public Document clone(){
    Document doc = new Document();
    List<Field> fields = doc.getFields()
    Field newField = null;
    for(Field f: fields){
        newField = f.clone()
       doc.addField(newField);
    return doc;
    }Please let me know which is a better approach and the standard coding
    practise.
    Thanks

    the second method is better, because it lets you the possibility to create a subclass of Field with a specific clone method, integrate some instances in a Document object; without changing the Document.clone method.

  • Deep copy of String

    As String is called by reference, how can I perform deep copy?
    Anything similar to System.arraycopy for String?

    That's funny, this works for me:
    String k = "kkkkkkkk";
    String m = k;
    Now, when I change 'k' (f.i. k = k.substring(0, 2); )
    'm' does not change.
    So that's a deep copy!
    ;JOOP!No - it's not a deep copy. Your statement k = k.substring(0, 2) does not change the string that was referenced by k previously, but changes the k variable itself so that it references a new string.
    It's really important to get a handle on the difference between objects
    on the one hand, and references to them on the other. Without that understanding you have little hope of writing correct and efficient Java programs.
    Sylvia.

  • Copy Performance Point lists between environments

    Hello,
    I would like to know if there is a way to copy paste Performance Point content lists and Data Connexion between 2 SharePoint 2013 environments (different farms)
    Actually we use Dashboard Designer to create report, kpi, filter and Dashboard . We actually have many items in our production farm and would like to copy these elements into POC farm (same Active Directory, same Cube, OLAP)
    Is it possible ? We already success to copy Performance Point content in the same farm between 2 differents web application or Site collection.
    Best regards,

    Hi arimaze,
    How did you copy PerformancePoint content in the same farm?
    Whether you try to use "Export Content" and "Import Content" ribbon button in PERFORMANCEPOINT ribbon. Please do a test, and let me know the result.
    In addition, there are two articles about migrating PerformancePoint content, please check if they are useful for you:
    http://denglishbi.wordpress.com/2010/11/20/migrating-performancepoint-2010-content-to-new-server/
    http://blogs.msdn.com/b/performancepoint/archive/2012/08/03/what-s-new-in-performancepoint-services-2013.aspx (8.server-side migrate)
    Best Regrads,
    Wendy
    Wendy Li
    TechNet Community Support

  • Creating a deep copy

    I wrote a stack implementation using a LinkedList, I have null constructor to create the first stack and it works fine but now i have to create a copy for a second stack using the element of the first constructor
    this my code
    import java.util.LinkedList;
    public class Stack<T>{
      private LinkedList<T> stack;
      public Stack(){
        stack =  new LinkedList<T>();
      public Stack(Stack<T> s) {// the problem is here it compile but when i run
                                                     // the program the second stack raised an exception because is empty
                                                     // i want to create a deep copy with the elements of the first stack
         stack = new LinkedList<T>();
      public boolean isEmpty(){
      return stack.isEmpty();
      public void push(T item){
       /*method to add object information
        *pre the object is not full
        * pos: the object is inserted
          stack.addFirst(item);
      public T top() throws EmptyStackException{
        if (!stack.isEmpty()){ 
          return stack.getFirst();
        else{
          throw new EmptyStackException("StackException on top " + " Stack empty");
      public T pop() throws EmptyStackException{
        /*method to remove an element of the stack
        *pre the object is not empty
        * pos: the object is remove
        * response throws an exception
        if (!stack.isEmpty()){ 
          return stack.removeFirst();
        else{
          throw new EmptyStackException("StackException on top" + "stack empty");
    public int numElements(){
        return stack.size();
    public void clear(){
      stack.clear();
    public class EmptyStackException extends Exception{
            public EmptyStackException( String message){
              super(message);
    }

    wel well... are you doing this on a mobile phone???
    neways to get to ur question u can use the clone() method to generate a clone of your object which is the same as a deep copy for eg:
    public class CloneTest{
        CloneTest test1 = new CloneTest();
        CloneTest test2;
        CloneTest(){
          //Some initialization code;
        CloneTest(CloneTest temp){
             test2 = test1.clone();
    }clone is a method of the object class.

  • Help with a deep copy

    What is the best practice for sending doing a deep copy between two View Controllers in an iPhone app. I've designed an app that sends over arrays of arrays and the deeper items aren't coming over as easily as I thought. Do you explicitly copy? Just alloc placeholders and set them equal? Links to any samples would be greatly appreciated.
    Is it a best practice to avoid that kind of data passing - as being overly complex?
    Background:
    I'm building an iPhone application where I need to send a complex object from one view controller to another - This complex object is an array of arrays of objects.
    I can't just copy a reference to the complex object over to a variable on the receiving view controller, as none of the nested array contents come over.
    So I think I need to do a deep copy - but need advice on best practices for that. The array of arrays contain objects, so ideally, Id just be able to :
    Thanks for any advice!

    There are many ways of solving this.
    One way is to centralize the information:
    You create a singleton class that is the datasource for both view, so you don't need to pass in anything. If you can't do that because the datasources are generally different except for this specific array of arrays, then make the singleton class be the source of that array and nothing else.

  • Deep Copy

    Hi all,
    I have a question I can't find any documentation about, not on google, not here on the forums. We are developping a EJB J2EE application (with glassfish) with flex as the view. We are using BlazeDS to interchange objects back and forth between flex and java.
    The problem we are having is we have to do a deep copy of an object tree, but blaze by default copies shallow.
    The setup is like this:
    We generated the actionscript classes that represent the java classes on the client side with a code generator named Gas3 (the only one out there, no?). This generator created classes with RemoteClass and Bindable annotations already in place. Gas3 also implemented the IExternalizable interface but we threw that out because it gave us problems in java. We have our EJB services configured in the remoting-config.xml file and everything works except for deep copy so I don't think there is anything missing, no?
    Is somebody aware of a way we can achieve a deep copy? I imagine there would be an xml tag or attribute that would allow for this ...
    Any help would be greatly appreciated!
    Thank you,
    Bruno Windels
    3S

    Or is it creating a deep copy of each element in the Array and placing them into a new array object?

  • Possible ways to Imporve the client copy performance

    Hi All,
    Would you pleae suggest all the ways to improve the client copy performance and how I can improve teh Cc performance which is already started.
    Thanks & Regards
    Rajesh Meda

    I don't think is portal related. I suggest you ask in one of the admin forums.

  • How to deep copy a component configuration?

    Hi Experts,
    I want to deep copy a component configuration named EHHSS_RAS_OIF.
    But following was the error shown.
    Can you please help/guide to do the same.
    Thanks in advance.
    Sanket.

    Hi Sanket,
    I think you might be copying the config in Unit test client but not in Development client.
    Thanks
    KH

  • Shallow copying and Deep copying ???

    hi,
    what is meant by shallow copying and deep copying in context to clone() method found in Object class?
    thanks,
    swapnaja

    swapnaja wrote:
    Shallow copying: only cloneable object is copied but any object references within that object would not be copied but would rather be shared.
    Deep copying: cloneable object along with any object references contained within that object will also be copied.
    Am i right?Yep; when you want to make a deep copy you have to watch out for circular references, i.e. if you have made a copy of an object already you shouldn't make another one during the deep copying process; (see how the ObjectOutputStream does things).
    kind regards,
    Jos

  • Shadow copy vs deep copy

    hi,
    any body tell me what is the difference between shadow copy and deep copy of a object.

    would you tell me any prectical example of this. i
    mean when i should use shallow copy and when i should
    use deep copy of object.I'm hoping someone else will step in here, because in my OO thinking, there's seldom a need for copying an object at all.
    One practical example, though: Say you're copying a list of objects. After the copy, you want to alter the objects in the copied list, but you don't want the objects in the original list altered. In this case, you need a deep copy. Had you only done a shallow copy, both lists would have referred to the same objects, and therefore, any change in an object would be visible through both lists.
    clone() method makes shallow copy or deep copy.You'd have to consult the doc for the class you are cloning. For example, the doc for LinkedList.clone() says "Returns a shallow copy of this LinkedList."

  • How does deep copy apply to arrays?

    I have a service object that has a method that returns an array of
    objects. The return type for the method is defined with the copy option.
    I found documentation that states that the copy option creates a deep
    copy of the return variable on the partition that called the method.
    My question is: If the return type for the method is an array of
    objects, will the copy option create copies of all objects/elements in
    the array?
    We wish to eliminate any object references to the service object's
    partition. Any insight would be greatly appreciated.
    Thanks in advance,
    Van Vuong
    Phone: 972.985.5289
    Pager: 972.320.2232
    VoiceNow Pager: 972.330.0822
    E-mail: [email protected]
    PAGE NET

    Copy option always copies deep. Remember, also if you pass the array
    accross partitions, whether you specify copy or not, it is going to copy
    and copy deep.
    In an array, I am not sure if have the problem, because unless the array
    in-turn holds a huge tree, the array object may be wide, but not deep.
    Some thing to think about??
    Venkat

  • File copy performance

    I measure file copy performance when copying big files (50Mb) to and from a IFS folder and the same to and from a Windows file server: it is about 4 times longer (30 s vs 7).
    All Oracle components are installed on the same windows NT machine (this is a test environment). The copies are done from a winXP machine on the \root ifs drive with smb protocol and on another windows shared folders on the same machine.
    Is there some configuration problem? Is the latest 9.0.4.1.1 CS better in that area?
    Tank’s in advance for any help.

    The most likely cause would be the issues mentioned in the Admin Guide Troubleshooting chapter under "Server is slow only on write activity". See:
    http://download-west.oracle.com/docs/cd/B10191_01/files.903/a97358/trouble.htm#1007182
    Because the File write is protected by a database transaction, it is being written in two places: Both to the Redo logs and to the DBF files. So ensure your Redo logs are large enough to hold an hour worth of document inserts, and put them on separate disks from your DBF files if at all possible.
    If that doesn't improve the performance, please create a new posting.

  • ObjectUtil doesnt deep copy correctly

    I am using an ObjectProxy for a complex object containing
    arrays of objects. I bind a datagrid to the array and . notation to
    fields. This all works fine, datagrid behaiving properly, inserts
    and deletes working properly.
    I want to have an undo function that will put the objectproxy
    object back to its original state. So when i open the screen with
    the datagrid I do a ObjectUtil.copy() and use this latter if the
    user wants to revert changes. The problem is that the copy doesn't
    do a deep copy. In the Object is an ArrayCollection of userdefined
    objects. These objects instead of being copied as their type are
    being copied as plain objects.
    The serialize meathod the copy uses doesn't retain the types
    of user defined objects.
    Will I have to create clone or copy methods for every object
    type I have?
    If so do I just need to make them ezxternalizable? How will
    the objectutil copy pick up these methods?
    Thanks,
    Jeff

    Unfortunatlly you will need to implement it for every class
    that has complex objects, as it does not know how to deal with
    them. Your classes need to extend EventDispatcher and/or implement
    IExternalizable.
    Then create methods writeExternal and readExternal for every
    local variables you need to retain. eg..
    public function writeExternal(output:IDataOutput):void {
    output.writeUTF(msgString);
    output.writeObject(anyObject);
    output.writeInt(id);
    public function readExternal(input:IDataInput):void {
    msgString = input.readUTF()as String;
    anyObject = input.readObject()as Array;
    id = input.readInt() as int;
    }

  • Deep copy a PriorityQueue

    Dear all,
    I am struggle with deep copying a priorityQueue. Anybody has a clue about his. It seems onyl swallow copy is allowed.
    Cheers,
    Ke

    See
    http://java.sun.com/docs/books/tutorial/java/IandI/objectclass.html
    http://www.google.com/search?num=100&hl=en&c2coff=1&q=java+deep+shallow+copy&btnG=Search

Maybe you are looking for

  • How to examine pdf for image file formats, OCR, and layers

    Hi, I have a question about how to find out specific features of a pdf.  I am using Adobe Acrobat 9 Pro for windows. With any given PDF, I am looking to find out: a]  The specs of any image files used  to create the pdf (i.e., if the pdf is made up o

  • After updating to iOS 5, the Photo album stopped working.

    After updating to iOS v5, my iphone 4S's built-in Photo app stopped working. The Camera takes a photo, but the photo disappears from the camera roll and shows no photos in the photoalbum. My iPhone used to take photos so, I don't believe it is a hard

  • Problem with receiver RFC.

    Hello all, I am getting below exception does anyone came across such exception, Please help me in getting out of this. Delivering the message to the application using connection RFC_http://sap.com/xi/XI/System failed, due to: com.sap.engine.interface

  • RandomAccessFile

    This class does not have a writeObject or readObject method like the ObjectOutputStream and the ObjectInputStream How am I supposed to write and read objects using random access files? Is there anyway besides just manually writing every primitive dat

  • Encode and Decode Base64

    Hi All, I need to Encode and Decode Base64 File.  Please let me know if there is any Function Modules or Class Methods to achieve the requirement. Thanks in advance. Regards Joseph Message was edited by: Joseph Brown