• Post Reply Bookmark Topic Watch Topic
  • New Topic

large HashMap vs. multiple small ones  RSS feed

 
Mark Stein
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kind of a generic question, but I'm just wondering if I'm better off with one large static hash map or breaking it into a few smaller ones. It would be for a web app.
Thanks.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As a rule, I'd suggest Do The Simplest Thing That Can Possibly Work until somebody proves it won't work - ie a real profiling tool shows the code takes too much time. As a good design and hedge if you're worried, hide the implementation behind a simple interface or wrapper class so that if you have to change it from one map to multiple maps later no other classes are broken.
 
John Smith
Ranch Hand
Posts: 2937
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kind of a generic question, but I'm just wondering if I'm better off with one large static hash map or breaking it into a few smaller ones.
Are you storing homogenous data in your map? Forget Java and programming, -- would you put these things into one or several different containers?
 
Mark Stein
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It could be one container, but if I wanted to, I could break it down into multiple containers. Just like you could have "cars", and you could break that down into different models of cars.
For now, the top level container would be fine, but if I use one map, I'll only be using a portion of it at a time (depending on which JSP page is called.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then it depends mainly on the distribution of the objects hash codes. If they are well distributed, it shouldn't make a difference.
 
John Smith
Ranch Hand
Posts: 2937
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ilja: Then it depends mainly on the distribution of the objects hash codes. If they are well distributed, it shouldn't make a difference.
Sounds to me like Mark is having a design problem, not a performance problem (although the fact that he posted here may suggest otherwise). I wouldn't think in terms of the distribution of the hash codes if the question is "what is a better design?". Perhaps Mark can clarify.
 
Mark Stein
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Eugene, you got it right. I'm in the design phase of a project right now, and was looking to factor in efficiency as I lay stuff out.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mark Stein:
Eugene, you got it right. I'm in the design phase of a project right now, and was looking to factor in efficiency as I lay stuff out.

That's a very slippery slope. It's much better to lay out stuff for maintenance and tackle performance problems as they arise.
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm with Ilja on this one. The first rule of coding for performance is "don't". The second rule is "follow the first rule unless you are an expert AND are having performance problems."
 
John Smith
Ranch Hand
Posts: 2937
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
IP: That's a very slippery slope. It's much better to lay out stuff for maintenance and tackle performance problems as they arise.
I am in full agreement with that.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To make the advice a little bit more practical:
Design your system so that only a minimal part of it knows about the number of HashMaps you use - that is, encapsulate that knowledge into a single place. That way it will be easy to switch between those two implementation details once you have real data on performance issues.
Hope this helps...
 
Mark Stein
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It does. Thanks a lot.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!