Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2006

Tip: Looking for answers? Try searching our database.

HashSet and TreeSet

Thread view: 
Ye Dafeng - 15 Nov 2006 05:40 GMT
I am searching for job now and I got a question between HashSet and TreeSet.
The question is: serialize the HashSet and TreeSet, then deserialize
them, what is the difference between these two sets.

I could not answer the question. After the interview,  I think about the
question carefully, I think after deserializing, the HashSet may not be
reconstructed because the hash code can not be calculated. Is that right?
Manish Pandit - 15 Nov 2006 08:22 GMT
> I could not answer the question. After the interview,  I think about the
> question carefully, I think after deserializing, the HashSet may not be
> reconstructed because the hash code can not be calculated. Is that right?

One difference I can think of is the order - HashSet does not guarantee
the order of its elements, while a TreeSet has its elements sorted in
their natural order. But that difference could be present before
serialization as well.

-cheers,
Manish
Robert Klemme - 15 Nov 2006 09:09 GMT
> I am searching for job now and I got a question between HashSet and TreeSet.
> The question is: serialize the HashSet and TreeSet, then deserialize
[quoted text clipped - 3 lines]
> question carefully, I think after deserializing, the HashSet may not be
> reconstructed because the hash code can not be calculated. Is that right?

There is a ton of differences between the two that is totally
independent of serialization (order, lookup times etc.).  The only thing
affected by serialization that I can think of relates to the internal
structure - it is likely that the internal hash table of the HashMap
looks differently (size wise) before serialization and after
deserialization - but then again this is just a difference for the pre
and post HashMap instances.  You can look into the source code of the
JDK to find out.  Other than that I would consider that this might be a
trick question.

Kind regards

    robert
Chris Uppal - 15 Nov 2006 12:57 GMT
> I am searching for job now and I got a question between HashSet and
> TreeSet. The question is: serialize the HashSet and TreeSet, then
> deserialize
> them, what is the difference between these two sets.

The only difference I can think of is that serialising/deserialising a HashSet
is likely to change the order of items seen by iterating over it (since it
rehashes the entire collection), whereas a TreeSet has a fixed, defined, order
which is only dependent on the contents of the Set.

It's not a /big/ difference, but it exists.

   -- chris
Mike  Schilling - 16 Nov 2006 03:00 GMT
>> I am searching for job now and I got a question between HashSet and
>> TreeSet. The question is: serialize the HashSet and TreeSet, then
[quoted text clipped - 7 lines]
> order
> which is only dependent on the contents of the Set.

I was going to correct you, but I'm quite wrong :-)

I presumed that TreeSet reinserts all of the members, in case the comparison
function depends on, say, the default string encoding.  (Say, if you're
going to sort strings by the lexicographic order of their encoded
representation.)  In that case, the order might have changed.  Looking at
the code, though, I see this is incorrect; TreeSet.readObject() assumes that
TreeSet.writeObject() wote the members in the correct order, and it
preserves that order.  So another answer is that serializing and
deserializing a HashSet always creates a correct object, but serializing and
deserializing  a TreeSet may not.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.