HashSet和LinkedHashSet有什么区别 [英] what's the difference between HashSet and LinkedHashSet

查看:333
本文介绍了HashSet和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.

该问题中有一个不幸的错误.我写了HashMapLinkedHashMap而不是HashSetLinkedHashSet.我已解决问题,如果可能的话,请回答.
另外,我对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.

LinkedHashMapHashMap,其中添加了双向链接列表实现.

The LinkedHashMap is an HashMap with a doubly-linked list implementation added.

这篇关于HashSet和LinkedHashSet有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆