HashSet and TreeSet

Y

Ye Dafeng

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?
 
M

Manish Pandit

Ye said:
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
 
R

Robert Klemme

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?

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
 
C

Chris Uppal

Ye said:
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
 
M

Mike Schilling

Chris Uppal said:
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.

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
474,269
Messages
2,571,100
Members
48,773
Latest member
Kaybee

Latest Threads

Top