简体   繁体   English

HashSet和Set之间有什么区别?

[英]What's the difference between HashSet and Set?

Saw the code snippet like 看到代码片段就像

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

I am wondering if Hashset is a special kind of set. 我想知道Hashset是否是一种特殊的集合。 Any difference between them? 他们之间有什么区别?

A Set represents a generic "set of values". Set表示通用的“值集”。 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. TreeSet通常实现为红黑树(请参阅http://en.wikipedia.org/wiki/Red-black_tree - 我没有验证sun / oracle的TreeSet的实际实现),而HashSet使用Object.hashCode()在数组中创建索引。 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) . 红黑树的访问时间是O(log(n))HashSet访问时间范围从常量时间到最差情况(每个项目具有相同的hashCode),其中您可以具有线性搜索时间O(n)

HashSetSet的实现。

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()),那么如果你发现这样做会更好,那么改变对象的实现是微不足道的,因为你在代码中只提到过一次base(你在那里做了新的HashSet())。

Set是类集合集合的通用接口,而HashSet是Set接口的特定实现(使用哈希码,因此名称)。

Set is a collection that contains no duplicate elements. Set是一个不包含重复元素的集合。 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. 由于HashSetSet接口的特定实现之一。

A Set can be any of following since it was implemented by below classes Set可以是以下任何一个,因为它是由下面的类实现的

ConcurrentSkipListSet : A scalable concurrent NavigableSet implementation based on a ConcurrentSkipListMap . ConcurrentSkipListSet :基于一个可扩展的并发NavigableSet实现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. 集合的元素按照它们的自然顺序进行排序,或者根据设置的创建时间提供的Comparator排序,具体取决于使用的构造函数。

CopyOnWriteArraySet : A Set that uses an internal CopyOnWriteArrayList for all of its operations. CopyOnWriteArraySet :一个使用内部CopyOnWriteArrayList进行所有操作的Set。

EnumSet : A specialized Set implementation for use with enum types. EnumSet :与枚举类型一起使用的专用Set实现。 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. TreeSet :基于TreeMap的NavigableSet实现。 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. LinkedHashSet :Set接口的ash表和链表实现,具有可预测的迭代顺序。 This implementation differs from HashSet in that it maintains a doubly-linked list running through all of its entries. 此实现与HashSet的不同之处在于它维护了一个贯穿其所有条目的双向链表。

But HashSet can be only LinkedHashSet since LinkedHashSet subclasses HashSet 但是HashSet只能是LinkedHashSet因为LinkedHashSet HashSet子类

Set is a parent interface of all set classes like TreeSet, LinkedHashSet etc. Set是所有集合类的父接口,如TreeSet,LinkedHashSet等。

HashSet is a class implementing Set interface. HashSet是一个实现Set接口的类。

HashSet is a class derived from Set interface. HashSet是从Set接口派生的类。 As a derived class of Set, the HashSet attains the properties of Set. 作为Set的派生类,HashSet获得Set的属性。 Important and the most frequently used derived classes of Set are HashSet and TreeSet. 重要的和最常用的Set派生类是HashSet和TreeSet。

** **

  • Set: 组:

** It is an interface which is a subtype of Collection interface, just like LIST and QUEUE. **它是一个接口,它是Collection接口的子类型,就像LIST和QUEUE一样。

Set has below 3 subclasses, it is used to store multiple objects without duplicates. Set有3个以下的子类,用于存储多个没有重复的对象。

  1. HashSet HashSet的
  2. LinkedHashSet LinkedHashSet
  3. TreeSet(which implements SortedSet interface) TreeSet(实现SortedSet接口)

** **

  • HashSet: HashSet的:

** **

Can use one NULL value(as Duplicate is not allowed), data is stored randomly as it does not maintain sequence. 可以使用一个NULL值(不允许复制),数据随机存储,因为它不保持序列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM