Code Comments
Programming Forum and web based access to our favorite programming groups.Hello everyone, I want to clone the content of a Hashtable (its keys, its values and its key-value relationship). But I have found that the clone method can not completely clone a Hashtable, as is mentioned in the description of clone method of Java API Specification, Creates a shallow copy of this hashtable. All the structure of the hashtable itself is copied, but the keys and values are not cloned. This is a relatively expensive operation. I am wondering what is the efficient and effective approach of cloning a Hashtable, including the clone of its keys, its values and its key-value relationship. Thanks in advance, George -- Message posted via http://www.webservertalk.com
Post Follow-up to this message"George George via webservertalk.com" <forum@nospam.webservertalk.com> wrote in news:bb9ee96855a04663b0832e6975ba5d2f@Ja vaKB.com: > Hello everyone, > > > I want to clone the content of a Hashtable (its keys, its values and > its key-value relationship). But I have found that the clone method > can not completely clone a Hashtable, as is mentioned in the > description of clone method of Java API Specification, > > Creates a shallow copy of this hashtable. All the structure of the > hashtable itself is copied, but the keys and values are not cloned. > This is a relatively expensive operation. > > I am wondering what is the efficient and effective approach of cloning > a Hashtable, including the clone of its keys, its values and its > key-value relationship. > > > Thanks in advance, > George > Perhaps something like this would work (correct me if I'm wrong): HashTable clone = originalHashTable; -- ------------- - ByteCoder - ...I see stupid people ------------- Curiosity *Skilled* the cat
Post Follow-up to this message"ByteCoder" <me@none.com> wrote in message news:Xns9657840B62E5FBCfromHolland@213.75.12.135... > "George George via webservertalk.com" <forum@nospam.webservertalk.com> wrote in > news:bb9ee96855a04663b0832e6975ba5d2f@Ja vaKB.com: > > > Perhaps something like this would work (correct me if I'm wrong): > > HashTable clone = originalHashTable; > > -- > ------------- > - ByteCoder - ...I see stupid people > ------------- > Curiosity *Skilled* the cat Yes, that is wrong. It will perform a reference assignment, and nothing more. Cloning a Hashtable is indicative of something seriously broken, and due to the common misconception to the contrary, I won't bother justifying the assertion for fear of boring, useless internet arguments. -- Tony Morris Software Engineer, IBM Australia. BInfTech, SCJP 1.4, SCJP 5.0, SCJD http://www.jtiger.org/ JTiger Unit Test Framework for Java http://qa.jtiger.org/ Java Q&A (FAQ, Trivia) http://xdweb.net/~dibblego/
Post Follow-up to this message"Tony Morris" <not@telling.you> wrote in news:428731d1@quokka.wn.com.au: > > "ByteCoder" <me@none.com> wrote in message > news:Xns9657840B62E5FBCfromHolland@213.75.12.135... > > Yes, that is wrong. It will perform a reference assignment, and > nothing more. > Cloning a Hashtable is indicative of something seriously broken, and > due to the common misconception to the contrary, I won't bother > justifying the assertion for fear of boring, useless internet > arguments. > Thank you for pointing that out to me. So you're actually saying there is no really no proper way to clone a HashSet? Actually, you could create a new HastSet and copy each item from the old HashSet to the new one. Granted, it's not as fast as 'cloning' it, but it should work. -- ------------- - ByteCoder - ...I see stupid people ------------- Curiosity *Skilled* the cat
Post Follow-up to this messageThanks Tony, I have written a simple sample below, but from the sample's output, I have found out that it seems that the clone method of Hashtable copies the the content of both keys and values, not as mentioned in the Java API specification of clone method of class Hashtable "but the keys and values are not cloned". Do you know what points do the Java specification mean?Output is: Contains! New value1 of table1: value1 New value1 of table2: value2 regards, George -- Message posted via http://www.webservertalk.comcode:
Hashtable table1 = new Hashtable(); table1.put ("key1", "value1"); Hashtable table2 = (Hashtable) table1.clone(); if (table2.containsKey ("key1")) { System.out.println ("Contains!"); table2.put ("key1", "value2"); String newValue1 = (String) table1.get ("key1"); System.out.println ("New value1 of table1: " + newValue1); newValue1 = (String) table2.get ("key1"); System.out.println ("New value1 of table2: " + newValue1); } else { System.out.println ("Not contains!"); }
Post Follow-up to this messageOn Mon, 16 May 2005 06:57:51 GMT, the cup of "George George via webservertalk.com" <forum@webservertalk.com> overfloweth with the following: > Thanks Tony, > > > I have written a simple sample below, but from the sample's output, I have > found out that it seems that the clone method of Hashtable copies the the > content of both keys and values, not as mentioned in the Java API > specification of clone method of class Hashtable "but the keys and values > are not cloned". > > Do you know what points do the Java specification mean? > >> > Output is: > > Contains! > New value1 of table1: value1 > New value1 of table2: value2 > > > regards, > George The clone() method creates new references to the same objects that are used as keys/values for the original. All you are achieving is assigning a different object to the same key. Forgive my poor ASCII art, but this is roughly what I mean: Original -------- Hashtable1 Key1 -------------> "key1" <------ Value1 -----------> "value1" <- | Hashtable2 | | Key1 -------------------------)--- Value1 ----------------------- Modified -------- Hashtable1 Key1 -------------> "key1" <------ Value1 -----------> "value1" | Hashtable2 | Key1 ----------------------------- Value1 -----------> "value2" Stancode:
> Hashtable table1 = new Hashtable(); > table1.put ("key1", "value1"); > Hashtable table2 = (Hashtable) table1.clone(); > if (table2.containsKey ("key1")) > { > System.out.println ("Contains!"); > table2.put ("key1", "value2"); > String newValue1 = (String) table1.get ("key1"); > System.out.println ("New value1 of table1: " + newValue1); > newValue1 = (String) table2.get ("key1"); > System.out.println ("New value1 of table2: " + newValue1); > } > else > { > System.out.println ("Not contains!"); > } >
Post Follow-up to this messageThanks Stan, G Winstanley wrote: >[quoted text clipped - 33 lines] > >The clone() method creates new references to the same objects that are used >as keys/values for the original. All you are achieving is assigning a >different object to the same key. > >Forgive my poor ASCII art, but this is roughly what I mean: > >Original >-------- >Hashtable1 > Key1 -------------> "key1" <------ > Value1 -----------> "value1" <- | >Hashtable2 | | > Key1 -------------------------)--- > Value1 ----------------------- > >Modified >-------- >Hashtable1 > Key1 -------------> "key1" <------ > Value1 -----------> "value1" | >Hashtable2 | > Key1 ----------------------------- > Value1 -----------> "value2" > >Stan What do you mean "All you are achieving is assigning a different object to the same key"? Could you please provide a sample program to illustrate your idea? regards, George -- Message posted via webservertalk.com http://www.webservertalk.com/Uwe/Forums.as...-setup/200506/1
Post Follow-up to this messageOn Thu, 09 Jun 2005 07:49:20 GMT, the cup of "George George via webservertalk.com"
<forum@webservertalk.com> overfloweth with the following:
> Thanks Stan,
>
>
> G Winstanley wrote:
>
> What do you mean "All you are achieving is assigning a different object to
> the same key"? Could you please provide a sample program to illustrate you
r
> idea?
>
>
> regards,
> George
Sure. This illustrates a bit of both...
import java.util.*;
public class HashCloneTest
{
public static void main(String[] args)
{
Hashtable table1 = new Hashtable();
Thing x = new Thing(1);
table1.put("key", x);
System.out.println("table1: " + table1.get("key"));
Hashtable table2 = (Hashtable)table1.clone();
System.out.println("table2: " + table2.get("key"));
((Thing)table1.get("key")).setNum(2);
System.out.println("table1: " + table1.get("key"));
System.out.println("table2: " + table2.get("key"));
table2.put("key", new Thing(3));
System.out.println("table2: " + table2.get("key"));
}
private static final class Thing
{
private int num;
private Thing(int i) { setNum(i); }
private void setNum(int i) { this.num = i; }
public String toString() { return "Thing[" + num + "]"; }
}
}
Stan
Post Follow-up to this message> From: "George George via webservertalk.com" <forum@nospam.webservertalk.com> > I want to clone the content of a Hashtable (its keys, its values and > its key-value relationship). But I have found that the clone method > can not completely clone a Hashtable, as is mentioned in the > description of clone method of Java API Specification, ... Creates a > shallow copy of this hashtable. All the structure of the hashtable > itself is copied, but the keys and values are not cloned. ... There are only three "canonical" copy operations possible: - Copying the reference (pointer) but don't copy any of the structure. (Java does this by simple assignment of pointer variables) - Copy the structure of the container itself, but just copy references to all components within the container. (This is the shallow copy you are referring to.) - Copy all the structure as deep as it goes. Unfortunately there can be pointer loops, so if you do this by simple recursive algorithm you get a recursive loop causing stack overflow. Java solves this in the serialization mechanism by building a hash table of all items copied and when it encounters a pointer to an item already in the table it makes a back-reference instead of copying it a second time. (If this is the behaviour you want, why don't you just serialize to a string then deserialize back to a new structure which is an isomorphic copy of the original?) If you want anything more than a shallow copy but less than a full deep serialization, how do you expect Java to guess what you want?? You should write your own class which sub-classes Hashtable, and define whatever kind of very-deep-but-not-all-the-way cloning you want. By the way, for enlightenment, you really should read this: Linkname: P.S.: The Best of Intentions URL: http://www.nhplace.com/kent/PS/EQUAL.html It's from the point of view of lisp, but it applies equally well to java.
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.