HashSet and TreeSet

Discussion in 'Java' started by Ye Dafeng, Nov 15, 2006.

  1. Ye Dafeng

    Ye Dafeng Guest

    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?
     
    Ye Dafeng, Nov 15, 2006
    #1
    1. Advertising

  2. Ye Dafeng wrote:
    > 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
     
    Manish Pandit, Nov 15, 2006
    #2
    1. Advertising

  3. On 15.11.2006 06:40, Ye Dafeng wrote:
    > 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
     
    Robert Klemme, Nov 15, 2006
    #3
  4. Ye Dafeng

    Chris Uppal Guest

    Ye Dafeng wrote:

    > 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
     
    Chris Uppal, Nov 15, 2006
    #4
  5. "Chris Uppal" <-THIS.org> wrote in message
    news:455b1157$0$623$...
    > Ye Dafeng wrote:
    >
    >> 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.


    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.
     
    Mike Schilling, Nov 16, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sandip Chitale

    Re: JList customized with TreeSet

    Sandip Chitale, Aug 23, 2003, in forum: Java
    Replies:
    0
    Views:
    650
    Sandip Chitale
    Aug 23, 2003
  2. Rhino

    TreeSet size() Problem

    Rhino, Feb 21, 2005, in forum: Java
    Replies:
    2
    Views:
    657
    Anton Spaans
    Feb 22, 2005
  3. Marcin

    TreeSet and HashSet

    Marcin, Feb 2, 2007, in forum: Java
    Replies:
    18
    Views:
    7,189
  4. LastHope
    Replies:
    6
    Views:
    625
  5. Frederik
    Replies:
    9
    Views:
    2,635
    Kevin McMurtrie
    Oct 15, 2009
Loading...

Share This Page