[OCP 21 book] | [OCP 17 book] | [OCP 11 book] | [OCA 8 book] [OCP 8 book] [Practice tests book] [Blog] [JavaRanch FAQ] [How To Ask Questions] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Originally posted by Edwin Dalorzo:
Serialize the HashTable then deserialize it into a new Object. That's the only way I know with out having to iterate cloning every item.
Originally posted by Jeanne Boyarsky:
George,
You can loop through the set of keys in the HashTable. For each one, get the associated value. Then clone the key/value and put them in a new HashTable.
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?
Originally posted by Henry Wong:
Actually, this is a good example of why a shallow copy is fine for most cases of cloning a hashtable. The operations on the hashtable are just with references -- and will not change the object itself. The only time that you need a deep copy, is when you manipulate both the hashtable and the objects in the hashtable itself. For objects likes strings, it is fine because these objects can't be changed.
Henry
Do you mean shallow copy of clone related concepts means copy only the reference?
Could you please show me some sample source codes dealing with why shallow copy is not enough and deep copy is needed?
:::Test1
:::Test2
I am wondering whether all the keys and values are needed to be serializable if I want to use your method
It is highly appreciated if you could provide some sample source codes about your method. :-)
Originally posted by Edwin Dalorzo:
Notice how hashcodes are different, meaning different object from the original to the deserialized. Object of the list have to be serializable, of course.
Originally posted by Ragu Ram:
Result is
You guessed it right all the key/value pairs need to be serializable. I am not sure if having them Externalizable helps.
In the example given above deep copy is not possible as MyClass doesnt implement the Cloneable interface.
You guessed it right all the key/value pairs need to be serializable. I am not sure if having them Externalizable helps.
I have an issue dealing with your sample. I think if the hash code values of two objects are different, then I think the two objects are different (not pointing to the same memory space). But I am not quite sure about this point. Am I correct? Do you have any online materials dealing with hash code computation, which can show that if two objects are different, then the hash values are different?
As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the JavaTM programming language.)
Originally posted by Edwin Dalorzo:
Hi, George. First notice that I am using the System.identityHashCode() and not the hashCode method of the Objects stored in the list. That is because hashCode method of the objects stored in the list could have been overriden and idententityHashCode() guarantees it will return the same hashCode() as it would have been returned by the Object class if the hashCode method would have not been overriden (as it is in this case because the list contains String objects).
String a = new String("George");
String b = new String("George");System.out.println(a==b); //Should print false and identityHashCodes are different
Originally posted by George Lin
do you mean the content of the "cloned" table "my2ndTable" could be affected by the original object reference "m"?
Originally posted by George Lin
I do not agree with this point.I think we can copy MyClass objects manually, by creating new objects and set the values for each of its member variable field, even though it is not an efficient and intelligent method. I am wondering why you mentioned the "Cloneable" interface at this point?
Output is
:::Test1
:::Test1
I am wondering what is the use of Interface "Externalizable"
I have tested that the output is true, which is not the same as we imagined. Do you know what is the reason?
I have got all of your points except this one, the overriden issue. Could you please provide some sample cource codes to illustrate why identityHashCode is more convenient than hashCode method (I always use hashCode before)? What are your points when describing hashCode method by using String objects?
Originally posted by Ragu Ram:
[QB]
Originally posted by Edwin Dalorzo:
So, once you have overriden the equals method the only way you have to obatin the value of hashCode as it would have been originally returned by the Object.hashCode method is to call System.identityHashCode().
Different indentityHashCode would prove to objects are different, because that would prove that they are different memory references.
[ May 19, 2005: Message edited by: Edwin Dalorzo ][/QB]