🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.language
Article #15693

Re: collections and deep copy

#15693
From: Werner Dubitzky
Date: Tue, 11 May 1999 00:00
90 lines
2860 bytes
This is a multi-part message in MIME format.
--------------7ADA328A7BC42A14F8310AD5
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

 Your first line:

   List copy = new ArrayList(planets);

Produces a shallow copy.  Your second line:

   Collections.copy(copy,planets);

is an expensive no-op: it merely overwrites each element
with itself.

   As for deep copies, Java itself offers very little
support for them.  In
particular, the fact that the Cloneable interface lacks a
public clone
operation makes it impossible to do a deep copy of a
collection unless you
know the element type.  For this reason, the Java
Collections Framework
offers no direct support for deep copying, but it's quite
straightforward to
do it manually if you know the element type:

    copy = new ArrayList();
    for (Iterator i=planets.iterator(); i.hasNext(); )
        copy.add(((StringBuffer)(i.next())).clone());

But this raises an interesting question: why are you using
(mutable)
StringBuffers instead of (immutable) Strings?  The names of
the planets
won't change, and if you use immutable elements, there's no
need to do a
deep copy.  In general, I believe that it's good practice to
use immutable
elements (and more generally, immutable types) wherver
possible.

         Regards,

         Josh




Werner Dubitzky wrote:

> How can I make DEEP copies of collections within the
> Collections Framework?
>
> The two approaches below make only SHALLOW copies:
>
> ...
> List copy = new ArrayList(aList);
> ...
>
> ...
> Collections.copy(destList,sourceList);
> ....
>
> Cheers.
> Mabuse

--------------7ADA328A7BC42A14F8310AD5
Content-Type: text/x-vcard; charset=us-ascii;
 name="w.dubitzky.vcf"
Content-Transfer-Encoding: 7bit
Content-Description: Card for Werner Dubitzky
Content-Disposition: attachment;
 filename="w.dubitzky.vcf"

begin:vcard 
n:;Homepage
tel;fax:+44-(0)-1232-366068
tel;work:+44-(0)-1232-368330
x-mozilla-html:TRUE
url:http://www.infj.ulst.ac.uk/~cbbg23/interests.html
org:University of Ulster, Faculty of Informatics
adr:;;Co. Antrim BT37 OQB;;;;Northern Ireland
version:2.1
title:Dr. Werner Dubitzky
fn:Homepage
end:vcard

--------------7ADA328A7BC42A14F8310AD5--
Technical Details
Message-ID: <37387736.9F3F35BF@ulst.ac.uk>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <3738372E.6C32A2F9@ulst.ac.uk>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f74b3,d93055e5a2ac437a
X-Google-Attributes: gidf74b3,public
From: Werner Dubitzky <w.dubitzky@ulst.ac.uk>
Subject: Re: collections and deep copy
Date: 1999/05/11
Message-ID: <37387736.9F3F35BF@ulst.ac.uk>
X-Deja-AN: 476685656
References: <3738372E.6C32A2F9@ulst.ac.uk>
X-Accept-Language: de-DE
Content-Type: multipart/mixed; boundary="------------7ADA328A7BC42A14F8310AD5"
Organization: Another Netscape Collabra Server User
Mime-Version: 1.0
Newsgroups: borland.public.jbuilder.java.language