HashSet和LinkedHashSet有什么区别 [英] what's the difference between HashSet and LinkedHashSet
问题描述
我看到LinkedHashSet
扩展了HashSet
,并且我知道它保留了顺序.
但是,从检查JDK中的代码来看,LinkedHashSet
似乎仅包含构造函数,没有实现,因此我猜所有逻辑都发生在HashSet
中?
如果正确的话,为什么要这样设计?似乎很令人困惑.
I saw that LinkedHashSet
extends HashSet
and I know it preserves order.
However, from checking the code in the JDK it seems that LinkedHashSet
contains only constuctor and no implementation, so I guess all the logic happens in HashSet
?
If that is correct, why is it designed like that? it seems very confusing.
该问题中有一个不幸的错误.我写了HashMap
和LinkedHashMap
而不是HashSet
和LinkedHashSet
.我已解决问题,如果可能的话,请回答.
另外,我对Java设计师为什么选择像这样实现它感兴趣.
there was an unfortunate mistake in the question. I wrote HashMap
and LinkedHashMap
instead of HashSet
and LinkedHashSet
. I fixed the question answer it if possible.
Also, I was interested why Java designers chose to implement it like that.
推荐答案
是的,LinkedHashMap
调用了其超级构造函数.它要做的一件事就是重写由超级构造函数调用的init()
方法.
Yes, LinkedHashMap
calls its super constructor. One thing it does is to override the init()
method, which is called by the super constructor.
LinkedHashMap
是HashMap
,其中添加了双向链接列表实现.
The LinkedHashMap
is an HashMap
with a doubly-linked list implementation added.
这篇关于HashSet和LinkedHashSet有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!