This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes Why HashMap is transient inside HashSet implementation? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Why HashMap is transient inside HashSet implementation?" Watch "Why HashMap is transient inside HashSet implementation?" New topic
Author

Why HashMap is transient inside HashSet implementation?

Shishir R Verma
Greenhorn

Joined: May 01, 2012
Posts: 6

Hi,

This question might have been asked earlier and probably a dumb question to ask, but It is somehow stuck in my mind.
I was looking at the code of HashSet, which actually looks like a wrapper around HashMap. I noticed that the HashSet class is implementing Serializable and HashMap is declared as transient variable inside it. Why HashMap is transient?

Thanks in advance...
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7492
    
  18

Shishir Rmv wrote:I was looking at the code of HashSet, which actually looks like a wrapper around HashMap. I noticed that the HashSet class is implementing Serializable and HashMap is declared as transient variable inside it. Why HashMap is transient?

Almost certainly because a HashSet can be translated into a more compact serialized form than an equivalent HashMap; but at the end of the day, without talking to the designer, it's pure speculation.

Perhaps a bit more source-code gazing is in order.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Shishir R Verma
Greenhorn

Joined: May 01, 2012
Posts: 6

Winston Gutkowski wrote:
Almost certainly because a HashSet can be translated into a more compact serialized form than an equivalent HashMap; but at the end of the day, without talking to the designer, it's pure speculation.

Winston


Thanks Winston. That's what I was thinking. And in the end of the day while deserialzing it back, it loses its compactness anyways.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why HashMap is transient inside HashSet implementation?
 
Similar Threads
Why HashMap.Entry is transient?
5 simple J2EE test questions
What is a HashSet?
HashMap Vs. HashSet
How is serialization handled with transient variables defined in Collections ?