哈希图还是哈希集?
我有两个包含列表
List<MyObj>.
,并且 MyObj 有一个“字符串 ID”成员。
我需要时不时地迭代它们,有时我需要找到两者相似的对象。
我想要一种比列表更快的方法。所以我知道我可以使用 hashMap (比在比较时询问 contains (String ) )
我应该使用 hashmap 还是 hashset?
注意:在哈希集中 - 我需要实现我的 equals ,当我运行 contains() 时 - 我认为它会比 hashmap 慢,在 hashmap 中插入时我将字符串 id 放入键中。 我说得对吗?
I have two list containing
List<MyObj>.
and MyObj has a "String ID" member.
I need to iterate them from time to time and sometimes I need to find objects which are similar on both.
I want a quicker way than lists. so I can know I can use hashMap
(than ask contains (String ) when comparing )
should I use hashmap or hashset?
note: in a hashset - I need to do implement my equals and when I run contains() - i think it will be slower than hashmap where upon inserting I put the string id in the key.
Am I correct?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我认为您不会注意到任何性能差异。
HashSet
是在底层使用HashMap
实现的。因此,唯一的区别是调用MyObj.equals()
(据说调用String.equals()
)与调用String.equals()
直接地。 JIT 编译器非常擅长内联调用……最重要的是,您(几乎)不应该担心微优化,而应该专注于使您的设计简单且一致。如果您唯一关心的是避免重复并检查包含情况,那么
Set
是更合理的选择。I don't think you would notice any performance difference.
HashSet<E>
is implemented using aHashMap<E, E>
under the hood. So the only difference would be callingMyObj.equals()
(which supposedly callsString.equals()
) vs callingString.equals()
directly. And the JIT compiler is pretty good at inlining calls...The bottom line is, you should (almost) never worry about micro-optimizations, rather focus on making your design simple and consistent. If your only concern is to avoid duplication and to check for containment, a
Set
is a more logical choice.这实际上并没有什么区别,因为当您查看 JDK 源代码时,
HashSet
的 Sun 实现在内部使用HashMap
的实例来存储其值:即使情况并非如此,所有其他关于它与性能 POV 并没有真正产生区别的答案都适用。唯一真正的区别是,您需要编写自己的密钥类实现来使用 Set,而不是使用 equals() 和 hashCode() 实现,但这些可以就像委托给类的 id 字段一样简单(如果 id 字段是唯一标识符)。
This does not really make a difference at all, because when you look at the JDK source code, the Sun implementation of
HashSet
uses an instance ofHashMap
internally to store its values:And even if that was not the case, all other answers about that it does not really make a difference from a performance POV apply. The only real difference is that instead of using the
equals()
andhashCode()
implementations of your key class you need to write your own for using the Set - but those could be as simple as delegating to theid
field of your class, in case that theid
field is the unique identifier.好吧,使用 HashMap 您将被迫以这种方式存储数据:
这不是最好的方法,因为您已经在 MyObject 中拥有 ID 字段。
使用 HashSet,您将能够仅存储 MyObject 的唯一实例,并且您还需要在 MyObject 中实现 hashCode()。
由你来选择。
Well, using HashMap you will be forced to store data in this way :
That isn't the best way, because you already have ID field in MyObject.
Using HashSet you will be able to store only unique instances of MyObject and you also will need to implement hashCode() in MyObject.
It's up to you to choose.