-1

What is the difference between Set and Hash Set in java? When to use either of these?

itzmebibin
  • 9,199
  • 8
  • 48
  • 62
user3640657
  • 377
  • 2
  • 4
  • 9

3 Answers3

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

The HashSet is an implementation of a Set.

0

A Set represents a generic "set of values". A TreeSet 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