HashSet 与 LinkedHashSet
它们之间有什么区别?我知道
LinkedHashSet 是 HashSet 的有序版本, 维护一个跨所有元素的双向链接列表。使用此类代替 HashSet 当您关心迭代顺序时。当你迭代 HashSet 时 顺序是不可预测的,而 LinkedHashSet 允许您迭代元素 按照它们插入的顺序。
但是在LinkedHashSet的源代码中,只有调用HashSet的构造函数。那么双链表和插入顺序在哪里呢?
What is the difference between them? I know that
A LinkedHashSet is an ordered version of HashSet that
maintains a doubly-linked List across all elements. Use this class instead of HashSet
when you care about the iteration order. When you iterate through a HashSet the
order is unpredictable, while a LinkedHashSet lets you iterate through the elements
in the order in which they were inserted.
But in sourcecode of LinkedHashSet there are only calling constructors of HashSet. So where is double-linked List and insertion order?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
HashSet 是无序且未排序的集合。
LinkedHashSet 是 HashSet 的有序版本。
HashSet 和 LinkedHashSet 之间的唯一区别是:
LinkedHashSet 维护插入顺序。
当我们迭代HashSet时,顺序是不可预测的,而LinkedHashSet的顺序是可预测的。
LinkedHashSet 维护插入顺序的原因是:
底层使用的数据结构是双向链表。
HashSet is unordered and unsorted Set.
LinkedHashSet is the ordered version of HashSet.
The only difference between HashSet and LinkedHashSet is that:
LinkedHashSet maintains the insertion order.
When we iterate through a HashSet, the order is unpredictable while it is predictable in case of LinkedHashSet.
The reason for how LinkedHashSet maintains insertion order is that:
The underlying used data structure is Doubly-Linked-List.
LinkedHashSet
的构造函数调用以下基类构造函数:如您所见,内部映射是一个
LinkedHashMap
。如果您查看LinkedHashMap
内部,您会发现以下字段:这是有问题的链接列表。
LinkedHashSet
's constructors invoke the following base class constructor:As you can see, the internal map is a
LinkedHashMap
. If you look insideLinkedHashMap
, you'll discover the following field:This is the linked list in question.
我建议您大多数时候使用
LinkedHashSet
,因为它总体性能更好):HashMap
稍好,因为大多数时候我们使用Set结构进行迭代。您可以在此处查看源测试页面:最终性能测试示例
I suggest you to use
LinkedHashSet
most of the time, because it has better performance overall):HashMap
, because the most of the time we use Set structures for iterating.You can see source test page here: The Final Performance Testing Example
您应该查看它调用的
HashSet
构造函数的源代码...它是一个特殊的构造函数,使支持Map
成为LinkedHashMap
而不仅仅是一个HashMap
。You should look at the source of the
HashSet
constructor it calls... it's a special constructor that makes the backingMap
aLinkedHashMap
instead of just aHashMap
.HashSet
不维护插入项的顺序LinkedHashSet
维持插入项的顺序示例
HashSet
输出LinkedHashSet
输出HashSet
don't maintain the order of insertion itemLinkedHashSet
maintain the order of insertion itemExample
HashSet
outputLinkedHashSet
output哈希集:
实际上是无序的。
如果你传递参数意味着
Out Put:
可能
2,1,3
不可预测。下次再下单。LinkedHashSet()
产生 FIFO 顺序。HashSet:
Unordered actually.
if u passing the parameter means
Out Put:
May be
2,1,3
not predictable. next time another order.LinkedHashSet()
which produce FIFO Order.HashSet:
下划线的数据结构是Hashtable。
不允许重复的对象。插入顺序不会保留,并且基于对象的哈希码。
可以插入空值(仅一次)。
它实现了 Serializable、Clonable 但没有 RandomAccess 接口。
如果频繁的操作是查找操作,最好选择HashSet。
构造函数:
LinkedHashSet:
它是HashSet的子类。它与 HashSet 完全相同,包括(构造函数和方法),但有以下差异。
差异
HashSet:
LinkedHashSet:
HashSet:
The underlined data structure is Hashtable.
Duplicate objects are not allowed.insertion order is not preserved and it is based on hash code of objects.
Null insertion is possible(only once).
It implements Serializable, Clonable but not RandomAccess interface.
HashSet is best choose if frequent operation is search operation.
Constructors:
LinkedHashSet:
It is a child class of HashSet. it is exactly same as HashSet including(Constructors and Methods) except the following differences.
Differences
HashSet:
LinkedHashSet:
如果您查看从
LinkedHashSet
类调用的构造函数,您会发现在内部它是一个用于支持目的的LinkedHashMap
。If you take a look at the constructors called from the
LinkedHashSet
class you will see that internally it's aLinkedHashMap
that is used for backing purpose.所有方法和构造函数都是相同的,但只有一个区别是 LinkedHashset 将保持插入顺序,但不允许重复。
哈希集不会维护任何插入顺序。
它是 List 和 Set 的简单组合:)
All Methods and constructors are same but only one difference is LinkedHashset will maintain insertion order but it will not allow duplicates.
Hashset will not maintain any insertion order.
It is combination of List and Set simple :)
正如您所说,两者之间的区别是:
至于你的问题:
答案在于
LinkedHashSet
使用哪些构造函数来构造基类:以及采用布尔参数的
HashSet
构造函数(一个示例)被描述,看起来像这样:The difference between the two are, as you've stated:
As for your question:
The answer lies in which constructors the
LinkedHashSet
uses to construct the base class:And (one example of) a
HashSet
constructor that takes a boolean argument is described, and looks like this: