22

I have been reading/researching the reason why HashMapis faster than HashSet.

I am not quite understanding the following statements:

  1. HashMap is faster than HashSet because the values are associated to a unique key.

  2. In HashSet, member object is used for calculating hashcode value which can be same for two objects so equals() method is used to check for equality. If it returns false, that means the two objects are different. In HashMap, the hashcode value is calculated using the key object.

  3. The HashMap hashcode value is calculated using the key object. Here, the member object is used to calculate the hashcode, which can be the same for two objects, so equals() method is used to check for equality. If it returns false, that means the two objects are different.

To conclude my question:

  1. I thought HashMap and HashSet calculate the hashcode in the same way. Why are they different?

  2. Can you provide a concrete example how HashSet and HashMap calculating the hashcode differently?

  3. I know what a "key object" is, but what does it mean by "member object"?

  4. HashMap can do the same things as HashSet, and faster. Why do we need HashSet? Example:

    HashMap <Object1, Boolean>= new HashMap<Object1, boolean>();
    map.put("obj1",true);  => exist
    map.get("obj1");  =>if null = not exist, else exist
    
7
  • Hashset is built on HashMap. And Set is used for uniqueness. It is nota key value pair collection. Commented Apr 29, 2013 at 12:50
  • Yes. I know they implement different interface. But some people say that the hashset is using hashmap in backend. If that's the truth, why hashset will be slower than hashmap? Commented Apr 29, 2013 at 12:50
  • Who is saying a HashSet is slower than a HashMap? Is an apple slower than an orange? Commented Apr 29, 2013 at 16:38
  • 3
    try it out yourself.... if you don't believe so.. I was doing a online judge using hashset but time exceed. But I changed to hashmap, i passed. Commented Apr 30, 2013 at 4:50
  • 1
    Can you back up your claim with code samples you've tested, timings etc? Commented May 1, 2013 at 19:16

2 Answers 2

27

Performance:

If you look at the source code of HashSet (at least JDK 6, 7 and 8), it uses HashMap internally, so it basically does exactly what you are doing with sample code.

So, if you need a Set implementation, you use HashSet, if you need a Map - HashMap. Code using HashMap instead of HashSet will have exactly the same performance as using HashSet directly.

Choosing the right collection

Map - maps keys to values (associative array) - http://en.wikipedia.org/wiki/Associative_array.

Set - a collection that contains no duplicate elements - http://en.wikipedia.org/wiki/Set_(computer_science).

If the only thing you need your collection for is to check if an element is present in there - use Set. Your code will be cleaner and more understandable to others.

If you need to store some data for your elements - use Map.

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

3 Comments

What denis said. Also, you can wrap any Map with a set using Collections.newSetFromMap.
If it is a set of objects, and can be mapped by primitive types, it is better to use hashmap instead of hashsets as the it is much faster, hashing primitive keys is much faster than hashing objects.
HashMap doesn't work on primitive types, even if you use primitive types they are converted on the fly to Objects. Furthermore, HashSet is nothing more than a simple wrapper over HashMap, so cannot HashMap has about the same performance as HashSet.
21

None of these answers really explain why HashMap is faster than HashSet. They both have to calculate the hashcode, but think about the nature of the key of a HashMap - it is typically a simple String or even a number. Calculating the hashcode of that is much faster than the default hashcode calculation of an entire object. If the key of the HashMap was the same object as that stored in a HashSet, there would be no real difference in performance. The difference comes in the what sort of object is the HashMap's key.

1 Comment

this should be the best answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.