HashSet和Set的区别是什么?

时间:2022-05-10 13:25:28

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?

我想知道Hashset是不是一种特殊的集合,它们之间有什么区别吗?

7 个解决方案

#1


71  

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.

集合表示一般的“值集”。TreeSet是元素被排序(从而排序)的集合,HashSet是元素没有排序或排序的集合。

A HashSet is typically a lot faster than a TreeSet.

HashSet通常比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).

TreeSet通常实现为红黑树(参见http://en.wikipedia.org/wiki/Red-black_tree -我还没有验证sun/oracle的TreeSet的实际实现),而HashSet使用Object.hashCode()在数组中创建索引。红黑树的访问时间为O(log(n)),而HashSet的访问时间从常量时间到最坏的情况(每个条目都有相同的hashCode),在这些情况下可以使用线性搜索时间O(n)。

#2


27  

The HashSet is an implementation of a Set.

HashSet是集合的实现。

#3


14  

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

通常,您想要对在本例中设置的接口进行编码。因为如果您总是通过接口引用您的对象(除了新的HashSet())),那么稍后更改对象的实现是很简单的,如果您发现这样做会更好,因为您在代码库中只提到它一次(您在那里做了new HashSet())))。

#4


9  

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

Set是一个集类集合的通用接口,HashSet是Set接口的一个具体实现(使用哈希代码,因此命名)。

#5


3  

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

Set是所有Set类的父接口,如TreeSet、LinkedHashSet等。

HashSet is a class implementing Set interface.

HashSet是一个实现Set接口的类。

#6


2  

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

Set是不包含重复元素的集合。是一个接口。

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

HashSet实现Set接口,由哈希表(实际上是HashMap实例)支持。

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

因为HashSet是Set接口的特定实现之一。

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

由于ASet是由下面的类实现的,所以ASet可以是以下任何一个

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.

ConcurrentSkipListSet:基于ConcurrentSkipListMap的可伸缩并发NavigableSet实现。设置的元素按照它们的自然顺序进行排序,或者按照设置的创建时间提供的比较器进行排序,这取决于使用的构造函数。

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

CopyOnWriteArraySet:使用内部CopyOnWriteArrayList进行所有操作的集合。

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.

TreeSet:基于TreeMap的NavigableSet实现。元素使用它们的自然排序,或者由在设置创建时提供的比较器来排序,这取决于使用哪个构造函数。

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.

LinkedHashSet:设置接口的ash表和链表实现,具有可预测的迭代顺序。这个实现与HashSet不同之处在于,它维护一个双链表,该列表遍历它的所有条目。

But HashSet can be only LinkedHashSet since LinkedHashSet subclasses HashSet

但是HashSet只能是LinkedHashSet,因为LinkedHashSet子类HashSet是HashSet

#7


-1  

I think you should read the javadoc for HashSet

我认为您应该阅读HashSet的javadoc。

#1


71  

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.

集合表示一般的“值集”。TreeSet是元素被排序(从而排序)的集合,HashSet是元素没有排序或排序的集合。

A HashSet is typically a lot faster than a TreeSet.

HashSet通常比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).

TreeSet通常实现为红黑树(参见http://en.wikipedia.org/wiki/Red-black_tree -我还没有验证sun/oracle的TreeSet的实际实现),而HashSet使用Object.hashCode()在数组中创建索引。红黑树的访问时间为O(log(n)),而HashSet的访问时间从常量时间到最坏的情况(每个条目都有相同的hashCode),在这些情况下可以使用线性搜索时间O(n)。

#2


27  

The HashSet is an implementation of a Set.

HashSet是集合的实现。

#3


14  

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

通常,您想要对在本例中设置的接口进行编码。因为如果您总是通过接口引用您的对象(除了新的HashSet())),那么稍后更改对象的实现是很简单的,如果您发现这样做会更好,因为您在代码库中只提到它一次(您在那里做了new HashSet())))。

#4


9  

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

Set是一个集类集合的通用接口,HashSet是Set接口的一个具体实现(使用哈希代码,因此命名)。

#5


3  

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

Set是所有Set类的父接口,如TreeSet、LinkedHashSet等。

HashSet is a class implementing Set interface.

HashSet是一个实现Set接口的类。

#6


2  

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

Set是不包含重复元素的集合。是一个接口。

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

HashSet实现Set接口,由哈希表(实际上是HashMap实例)支持。

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

因为HashSet是Set接口的特定实现之一。

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

由于ASet是由下面的类实现的,所以ASet可以是以下任何一个

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.

ConcurrentSkipListSet:基于ConcurrentSkipListMap的可伸缩并发NavigableSet实现。设置的元素按照它们的自然顺序进行排序,或者按照设置的创建时间提供的比较器进行排序,这取决于使用的构造函数。

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

CopyOnWriteArraySet:使用内部CopyOnWriteArrayList进行所有操作的集合。

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.

TreeSet:基于TreeMap的NavigableSet实现。元素使用它们的自然排序,或者由在设置创建时提供的比较器来排序,这取决于使用哪个构造函数。

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.

LinkedHashSet:设置接口的ash表和链表实现,具有可预测的迭代顺序。这个实现与HashSet不同之处在于,它维护一个双链表,该列表遍历它的所有条目。

But HashSet can be only LinkedHashSet since LinkedHashSet subclasses HashSet

但是HashSet只能是LinkedHashSet,因为LinkedHashSet子类HashSet是HashSet

#7


-1  

I think you should read the javadoc for HashSet

我认为您应该阅读HashSet的javadoc。