• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

HashMap Vs. HashSet

 
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's a question.
Which implementation is more efficient ?
HashMap or HashSet.
Now, I know the first difference btwn the two is that HashMap is used for key/value pairs and HashSet is used to just store a set of values. Both are not thread safe.
The deal is this - We have a pair of key/values BUT due to some reason, the original designer went for a 1 column table that would hold these key/values as "key_value" in the single column.
Now, we have the option of either going for a HashSet to hold this data in memory or a HashMap with the key being the "key_value" and the value being "true".
I prefer the HashSet since all we might have to do is check whether the specified "key_value" exists in the table. That can be easily ( and may I add, lightly ) done by using the contains(Object o) method of the HashSet class. Besides, why unnecessarily have a Map when we don't need one !
Any thoughts on this would be much appreciated.
thanks
Himanshu
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First, let's reiterate the Three Rules Of Optimization again:
1. Don't.
2. (for the very experienced only) Don't - yet.
3. (if, and only *if* you *really* need to) Use a profiler to find the bottleneck - it isn't where you think it is.
So, use a Set if you need to store single values, use a Map if you need to store key/value pairs.
In fact, if you take the time to take a look at the source code of java.util.HashSet, you will see that internally it is simply using a HashMap... :roll:
 
Himanshu Jhamb
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Liked the optimization rules !
I did read on the API pages that HashSet uses HashMap underneath... the question then remains,
Keeping above in mind...
Apart from the 'thumb rule' of using HashMap for key-value pairs,
Are there any performance advantages of using HashSet over HashMap in this case ?
Thx for the prompt reply.
- Himanshu
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Himanshu Jhamb:

Are there any performance advantages of using HashSet over HashMap in this case ?


Well, as HashSet is using HashMap, I would be surprised if there were any relevant difference in performance. But if you really need to know, you should probably use a variation of rule 3: Write a simple benchmark and just try it.
Don't forget to post your results...
[ June 05, 2002: Message edited by: Ilja Preuss ]
 
Himanshu Jhamb
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Awwww... You know what Ilja... That was just what I was trying to avoid ...
Was hoping for a concrete answer from someone who has already benchmarked this. But, I guess I'll have to toil some to get to the answer. Of course, If I do that, I will post the results here...
thx. anyway
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I wouldn't spend too much time worrying about it. I gree with Ilja - strictly from a performance perspective, the two methods should be virtually identical. If they're not, it would only be because the HashMap is more complicated to use here, and so you will have more opportunities to accidentally introduce ineffecient code along the way. The HashSet implementation has already figured out how to do this for you as efficiently as reasonably possible, I think - why waste that? And more importantly, using a Set makes it simpler for others to understand your intent at a glance.
 
Put a gun against his head, pulled my trigger, now he's dead, that tiny ad sure bled
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic