What is the difference between Set
and Hash Set
in java?
When to use either of these?
Asked
Active
Viewed 1.8k times
-1

itzmebibin
- 9,199
- 8
- 48
- 62

user3640657
- 377
- 2
- 4
- 9
-
4do you know the difference between interfaces and classes? – Eran Mar 09 '16 at 07:15
-
Set is interface and HashSet is class which implements Set – Musaddique S Mar 09 '16 at 07:15
-
You use a `Set` if you want to create your own `Set`, because it´s an `interface`... – SomeJavaGuy Mar 09 '16 at 07:15
-
8http://stackoverflow.com/questions/5139724/whats-the-difference-between-hashset-and-set – sasikumar Mar 09 '16 at 07:16
-
possible duplicate of https://stackoverflow.com/questions/5139724/whats-the-difference-between-hashset-and-set – Vikrant Kashyap Mar 09 '16 at 07:20
3 Answers
5
Set
is an interface, HashSet
- implementation of interface. It is recommended to use interface instead of implementation when you declaring variables.
If go further into details, an interface in Java is a set of methods, and if some class wants to implement this interface, it must implement all of its methods.
The Set
interface represents a set of some objects, non-ordered, without random-element access. HashSet
- implementation of the Set
interface, based on the .hashCode()
function.

Sebastian Nielsen
- 3,835
- 5
- 27
- 43

saroff
- 688
- 9
- 20
0
A Set
represents a generic "set of values". A TreeSe
t is a set where the elements are sorted (and thus ordered), a HashSet
is a set where the elements are not sorted or ordered.
A HashSet
is typically a lot faster than a TreeSet
.
Refer this.

Community
- 1
- 1

itzmebibin
- 9,199
- 8
- 48
- 62