This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Java in General and the fly likes Why HashMap.Entry is transient? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "Why HashMap.Entry is transient?" Watch "Why HashMap.Entry is transient?" New topic
Author

Why HashMap.Entry is transient?

Patrio Jensen
Greenhorn

Joined: Feb 20, 2009
Posts: 10
Hi,

Inside java.util.HashMap class, I see a field transient Entry[] table which is used to store data(key/value pair in this case).

Can someone help me to understand why table is declared as transient?

I tried serializing the HashMap object as mentioned here and could successfully serialize and deserialize.

For that matter even HashTable.elementData, ArrayList.elementData are also declared transient.

Thanks.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

Most Map implementations write their data without the entries, as follows:
- the number of mappings first
- for each mapping, the key first, then the value

For instance, if you have a map with contents {key1=value1, key2=value2, key3=value3}, the contents will be written as follows:
3
key1
value1
key2
value2
key3
value3

This is a tad more efficient than writing the Entry[] table, because then the output would contain more overhead: the array itself, and each Entry element.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Patrio Jensen
Greenhorn

Joined: Feb 20, 2009
Posts: 10
Thanks Rob for quick reply.

I tried printing value from Entry[] table of HashMap using reflection and can see that it's storing data.

And also I don't see any data structure (other than Entry[] ) inside HashMap to store key/value pair.
Lorand Komaromi
Ranch Hand

Joined: Oct 08, 2009
Posts: 276
Patrio Jensen wrote:
And also I don't see any data structure (other than Entry[] ) inside HashMap to store key/value pair.


Just take a look at writeObject(), that's where the entries are serialized...


OCJP 6 (93%)
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

It still uses the Entry[] table internally during runtime. The transient keyword only means that the entries will not be serialized / deserialized by default. That's where the readObject and writeObject methods come in; they read / write the entries manually. From the Java 6 source code of HashMap:
s.defaultWriteObject() writes the threshold and loadFactor fields; all other fields are transient. Likewise, s.defaultReadObject() reads those two fields again. The Entry[] table is simply reconstructed instead of serialized and deserialized.>
Patrio Jensen
Greenhorn

Joined: Feb 20, 2009
Posts: 10
Got it now.

Thanks Lorand, Rob.
 
Don't get me started about those stupid light bulbs.
 
subject: Why HashMap.Entry is transient?