What's the difference between HashSet and Set?

JavaInterfaceSet

Java Problem Overview


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?

Java Solutions


Solution 1 - Java

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).

Solution 2 - Java

The HashSet is an implementation of a Set.

Solution 3 - Java

Set is a collection that contains no duplicate elements. Set is an interface.

HashSet implements the Set interface, backed by a hash table (actually a HashMap instance).

Since HashSet is one of the specific implementations of Set interface.

ASet can be any of following since it was implemented by below classes

ConcurrentSkipListSet : A scalable concurrent NavigableSet implementation based on a ConcurrentSkipListMap. The elements of the set are kept sorted according to their natural ordering, or by a Comparator provided at set creation time, depending on which constructor is used.

CopyOnWriteArraySet : A Set that uses an internal CopyOnWriteArrayList for all of its operations.

EnumSet : A specialized Set implementation for use with enum types. All of the elements in an enum set must come from a single enum type that is specified, explicitly or implicitly, when the set is created.

TreeSet :A NavigableSet implementation based on a TreeMap. The elements are ordered using their natural ordering, or by a Comparator provided at set creation time, depending on which constructor is used.

LinkedHashSet: ash table and linked list implementation of the Set interface, with predictable iteration order. This implementation differs from HashSet in that it maintains a doubly-linked list running through all of its entries.

But HashSet can be only LinkedHashSet since LinkedHashSet subclasses HashSet

Solution 4 - Java

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()).

Solution 5 - Java

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).

Solution 6 - Java

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

HashSet is a class implementing Set interface.

Solution 7 - Java

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.

Solution 8 - Java

**

  • 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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser496949View Question on Stackoverflow
Solution 1 - JavaErikView Answer on Stackoverflow
Solution 2 - JavavaughamView Answer on Stackoverflow
Solution 3 - JavaRavindra babuView Answer on Stackoverflow
Solution 4 - JavaMeBigFatGuyView Answer on Stackoverflow
Solution 5 - JavagmwView Answer on Stackoverflow
Solution 6 - JavaUmesh KView Answer on Stackoverflow
Solution 7 - JavaHemlata GehlotView Answer on Stackoverflow
Solution 8 - JavaAnonymous UserView Answer on Stackoverflow