dexterousashish
dexterousashish

Reputation: 443

HashSet internally uses hashmap for its implementation, then why is hashmap faster than hashset?

HashSet internally uses hashmap for its implementation, then why is hashmap faster than hashset?

I tried reading the above mentioned post in search but was unable to find a clear answer

Upvotes: 3

Views: 2096

Answers (2)

Maxim Shoustin
Maxim Shoustin

Reputation: 77910

in a HashMap you store element value pairs when in a Set you store only element.

When you call add(value) for Set actually you call put as well (for HashMap).

Set backed by HashMap

Upvotes: 0

James
James

Reputation: 8586

Because HashSet uses a HashMap. It must incur the cost of using a HashMap, plus the overhead of the HashSet itself.

Upvotes: 10

Related Questions