5

I have two list containing

List<MyObj>.   

and MyObj has a "String ID" member.

I need to iterate them from time to time and sometimes I need to find objects which are similar on both.
I want a quicker way than lists. so I can know I can use hashMap (than ask contains (String ) when comparing )

should I use hashmap or hashset?

note: in a hashset - I need to do implement my equals and when I run contains() - i think it will be slower than hashmap where upon inserting I put the string id in the key. Am I correct?

1
  • Sounds like Java -> Added the "java" tag. If it's not java, please re-tag your question accordingly. Commented May 17, 2011 at 8:32

3 Answers 3

5

note: in a hashset - I need to do implement my equals and when I run contains() - i think it will be slower than hashmap where upon inserting I put the string id in the key. Am I correct?

I don't think you would notice any performance difference. HashSet<E> is implemented using a HashMap<E, E> under the hood. So the only difference would be calling MyObj.equals() (which supposedly calls String.equals()) vs calling String.equals() directly. And the JIT compiler is pretty good at inlining calls...

The bottom line is, you should (almost) never worry about micro-optimizations, rather focus on making your design simple and consistent. If your only concern is to avoid duplication and to check for containment, a Set is a more logical choice.

Sign up to request clarification or add additional context in comments.

Comments

2

This does not really make a difference at all, because when you look at the JDK source code, the Sun implementation of HashSet uses an instance of HashMap internally to store its values:

   public class HashSet<E>
    extends AbstractSet<E>
    implements Set<E>, Cloneable, java.io.Serializable
{
    static final long serialVersionUID = -5024744406713321676L;

    private transient HashMap<E,Object> map;

    // Dummy value to associate with an Object in the backing Map
......

And even if that was not the case, all other answers about that it does not really make a difference from a performance POV apply. The only real difference is that instead of using the equals() and hashCode() implementations of your key class you need to write your own for using the Set - but those could be as simple as delegating to the id field of your class, in case that the id field is the unique identifier.

1 Comment

Not really because set uses key as value.
1

Well, using HashMap you will be forced to store data in this way :

<ID1><MyObject> 
<ID2><MyObject>

That isn't the best way, because you already have ID field in MyObject.

Using HashSet you will be able to store only unique instances of MyObject and you also will need to implement hashCode() in MyObject.

It's up to you to choose.

2 Comments

I wonder if asking contains - over a hashset - calculates hashcode on all . and when asking contains() on hashmap - already holds an index and fetch it quicker.
HashSet doesn't calculates item's hash each time. I don't think that performance difference between HashSet and HashMap is noticeable. There is difference in the way they store the data.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.