106

Saw the code snippet like

Set<Record> instances = new HashSet<Record>();

I am wondering if Hashset is a special kind of set. Any difference between them?

user2864740
  • 60,010
  • 15
  • 145
  • 220
user496949
  • 83,087
  • 147
  • 309
  • 426
  • 11
    You might want to check out the concept of [interfaces](http://download.oracle.com/javase/tutorial/java/concepts/interface.html) – Nikita Rybak Feb 28 '11 at 08:40

7 Answers7

130

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.

A TreeSet is typically implemented as a red-black tree (See http://en.wikipedia.org/wiki/Red-black_tree - I've not validated the actual implementation of sun/oracle's TreeSet), whereas a HashSet uses Object.hashCode() to create an index in an array. Access time for a red-black tree is O(log(n)) whereas access time for a HashSet ranges from constant-time to the worst case (every item has the same hashCode) where you can have a linear search time O(n).

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
Erik
  • 88,732
  • 13
  • 198
  • 189
  • Additionally, there are these general-purpose-implementations: LinkedHashSet (a variant of HashSet which preserves some order for the Iterator), ConcurrentSkipListSet (a threadsave SortedSet implementation), CopyOnWriteArraySet (a thread-safe variant optimized for "lots of reads, very seldom writes"), EnumSet (which works only on enum types for the elements, but then is even faster than HashSet). – Paŭlo Ebermann Feb 28 '11 at 22:36
  • 8
    @Erik: I request to edit your answer. TreeSet is sorted not ordered. HashSet = Unordered, TreeSet = sorted, LinkedHashSet = ordered. Please modify your answer accordingly – Rais Alam Jan 04 '13 at 05:40
  • Hashset can be slower if the implementation of hashCode is bad (e.g. always return the same hashcode) – Romain Hautefeuille Mar 30 '17 at 01:52
41

The HashSet is an implementation of a Set.

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
vaugham
  • 1,821
  • 1
  • 19
  • 38
19

The question has been answered, but I haven't seen the answer to why the code mentions both types in the same code.

Typically, you want to code against interfaces which in this case is Set. Why? Because if you reference your object through interfaces always (except the new HashSet()) then it is trivial to change the implementation of the object later if you find it would be better to do so because you've only mentioned it once in your code base (where you did new HashSet()).

Tony L.
  • 17,638
  • 8
  • 69
  • 66
MeBigFatGuy
  • 28,272
  • 7
  • 61
  • 66
10

Set is the general interface to a set-like collection, while HashSet is a specific implementation of the Set interface (which uses hash codes, hence the name).

gmw
  • 417
  • 3
  • 15
4

Set is a parent interface of all set classes like TreeSet, LinkedHashSet etc.

HashSet is a class implementing Set interface.

Umesh K
  • 13,436
  • 25
  • 87
  • 129
0

HashSet is a class derived from Set interface. As a derived class of Set, the HashSet attains the properties of Set. Important and the most frequently used derived classes of Set are HashSet and TreeSet.

Hemlata Gehlot
  • 341
  • 2
  • 12
-1

**

  • Set:

** It is an interface which is a subtype of Collection interface, just like LIST and QUEUE.

Set has below 3 subclasses, it is used to store multiple objects without duplicates.

  1. HashSet
  2. LinkedHashSet
  3. TreeSet(which implements SortedSet interface)

**

  • HashSet:

**

Can use one NULL value(as Duplicate is not allowed), data is stored randomly as it does not maintain sequence.