哈希图还是哈希集?

发布于 2024-11-07 20:59:34 字数 341 浏览 0 评论 0原文

我有两个包含列表

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

情感失落者 2024-11-14 20:59:34

注意:在哈希集中 - 我需要实现我的 equals ,当我运行 contains() 时 - 我认为它会比 hashmap 慢,在 hashmap 中插入时我将字符串 id 放入键中。我说得对吗?

我认为您不会注意到任何性能差异。 HashSet 是在底层使用 HashMap 实现的。因此,唯一的区别是调用 MyObj.equals() (据说调用 String.equals())与调用 String.equals()直接地。 JIT 编译器非常擅长内联调用……

最重要的是,您(几乎)不应该担心微优化,而应该专注于使您的设计简单且一致。如果您唯一关心的是避免重复并检查包含情况,那么 Set 是更合理的选择。

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?

I don't think you would notice any performance difference. HashSet<E> is implemented using a HashMap<E, E> under the hood. So the only difference would be calling MyObj.equals() (which supposedly calls String.equals()) vs calling String.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.

标点 2024-11-14 20:59:34

这实际上并没有什么区别,因为当您查看 JDK 源代码时,HashSet 的 Sun 实现在内部使用 HashMap 的实例来存储其值:

   public class HashSet<E>
    extends AbstractSet<E>
    implements Set<E>, Cloneable, java.io.Serializable
{
    static final long serialVersionUID = -5024744406713321676L;

    private transient HashMap<E,Object> map;

    // Dummy value to associate with an Object in the backing Map
......

即使情况并非如此,所有其他关于它与性能 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 of HashMap internally to store its values:

   public class HashSet<E>
    extends AbstractSet<E>
    implements Set<E>, Cloneable, java.io.Serializable
{
    static final long serialVersionUID = -5024744406713321676L;

    private transient HashMap<E,Object> map;

    // Dummy value to associate with an Object in the backing Map
......

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() and hashCode() implementations of your key class you need to write your own for using the Set - but those could be as simple as delegating to the id field of your class, in case that the id field is the unique identifier.

好久不见√ 2024-11-14 20:59:34

好吧,使用 HashMap 您将被迫以这种方式存储数据:

<ID1><MyObject> 
<ID2><MyObject>

这不是最好的方法,因为您已经在 MyObject 中拥有 ID 字段。

使用 HashSet,您将能够仅存储 MyObject 的唯一实例,并且您还需要在 MyObject 中实现 hashCode()。

由你来选择。

Well, using HashMap you will be forced to store data in this way :

<ID1><MyObject> 
<ID2><MyObject>

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文