What's the difference between HashSet and Set?

前端 未结 8 1985
独厮守ぢ
独厮守ぢ 2020-12-12 19:01

Saw the code snippet like

Set instances = new HashSet();

I am wondering if Hashset is a special kind of set. A

相关标签:
8条回答
  • 2020-12-12 19:10

    The HashSet is an implementation of a Set.

    0 讨论(0)
  • 2020-12-12 19:11

    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

    0 讨论(0)
  • 2020-12-12 19:19

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

    HashSet is a class implementing Set interface.

    0 讨论(0)
  • 2020-12-12 19:28

    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.

    0 讨论(0)
  • 2020-12-12 19:30

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

    0 讨论(0)
  • 2020-12-12 19:31

    **

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

    0 讨论(0)
提交回复
热议问题