Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

TreeMap Vs. HashMap  RSS feed

 
V Bose
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Only in terms of space required to store n elements, which would be more efficient between a TreeMap and a HashMap implementation ?
The value of n is not know beforehand and may increase during runtime.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
TreeMap.Entry has the following fields:



HashMap.Entry those:



So it looks like HashMap entries are a little bit more lightweight.

On the other hand, a HashMap will contain at least 25% additional null references (using the default load factor) and may need more memory at peak times when it has to grow.

Do you have a rough estimate of how big n typically will get? How big are the objects you put into the Map?
 
Gilberto
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had the idea that the XXX.Entry objects were created where they were required, and not used internally.

----------------------------
Gilberto
pollensa villas
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

In your haste to come in and ask a question, you seem to have missed reading our policy on display names, which quite clearly states that you must use a real (sounding) first and last name for your display name -- a single name is not enough. You can fix your display name here. Thanks for your cooperation!

As to your last statement: no, they're used internally.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Gilberto:
I had the idea that the XXX.Entry objects were created where they were required, and not used internally.


Interesting idea, but also wrong, as a simple look at the source code will tell you.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!