CuriousMind
CuriousMind

Reputation: 8903

Hash based implementation of Set in Java Collection Framework?

I am learning Java collection framework in Java, and got fair idea of various Classes and interfaces.

While going through Set interface, one of the implementations is HashSet (among others).

I am not able to understand what is the logic of implementing Set based on the Hash, what advantage does it serve?

Can anyone help me understand what is the need of Hash based implementation of Set in Java Collection Framework?

Upvotes: 1

Views: 213

Answers (1)

GhostCat
GhostCat

Reputation: 140407

Simple: performance and efficiency.

The key element of Sets is: uniqueness. You want to quickly be able to tell wether some x is in (x, y, z) or not.

And hashing is a very elegant and efficient way of approaching this problem.

That is all there is to this.

Upvotes: 5

Related Questions