为什么HashMap比HashSet更快?

编程入门 行业动态 更新时间:2024-10-16 02:30:54
本文介绍了为什么HashMap比HashSet更快?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我一直在阅读/研究为什么 HashMap 快于 HashSet 。

我不完全理解以下语句:

  • HashMap 比 HashSet 快,因为这些值与唯一键相关联。

  • <在 HashSet 中,成员对象用于计算哈希码值,对于两个对象可以相同,所以 equals()方法用于检查是否相等。如果它返回 false ,这意味着两个对象是不同的。在 HashMap 中,哈希码值是使用键对象计算的。
  • > HashMap 使用密钥对象计算哈希码值。在这里,成员对象用于计算哈希码,对于两个对象可以是相同的,因此使用 equals()方法检查是否相等。如果它返回 false ,则表示这两个对象是不同的。

    总结我的问题:

  • 我认为 HashMap 和 HashSet 以相同的方式计算哈希码。为什么它们不同?

  • 你可以提供一个具体的例子 HashSet 和 HashMap 计算哈希码的方式不同? 我知道关键对象是什么,但是成员对象?
  • HashMap 可以做与 HashSet code>,并且更快。为什么我们需要 HashSet ?例子:

    HashMap< Object1,Boolean> = new HashMap< Object1,boolean>(); map.put(obj1,true); =>存在 map.get(obj1); => if null = not exist,else exists

  • 如果您查看HashSet的源代码(至少JDK 6 ,7和8),它在内部使用HashMap,所以它基本上完成了你对样例代码的处理。

    所以,如果你需要一个Set实现,你可以使用HashSet,如果你需要一个Map - HashMap。使用HashMap代替HashSet的代码与使用HashSet的性能完全相同。

    选择正确的集合

    映射 - 将键映射到值(关联数组) - http:// en .wikipedia / wiki / Associative_array 。

    Set - 一个不包含重复元素的集合 - en.wikipedia/wiki/Set_(computer_science)。

    如果您需要收集的唯一东西是检查一个元素是否存在 - 请使用Set。您的代码将更清晰,更易于理解。

    如果您需要为元素存储一些数据,请使用Map。

    I have been reading/researching the reason why HashMapis faster than HashSet.

    I am not quite understanding the following statements:

  • HashMap is faster than HashSet because the values are associated to a unique key.

  • In HashSet, member object is used for calculating hashcode value which can be same for two objects so equals() method is used to check for equality. If it returns false, that means the two objects are different. In HashMap, the hashcode value is calculated using the key object.

  • The HashMap hashcode value is calculated using the key object. Here, the member object is used to calculate the hashcode, which can be the same for two objects, so equals() method is used to check for equality. If it returns false, that means the two objects are different.

  • To conclude my question:

  • I thought HashMap and HashSet calculate the hashcode in the same way. Why are they different?

  • Can you provide a concrete example how HashSet and HashMap calculating the hashcode differently?

  • I know what a "key object" is, but what does it mean by "member object"?

  • HashMap can do the same things as HashSet, and faster. Why do we need HashSet? Example:

    HashMap <Object1, Boolean>= new HashMap<Object1, boolean>(); map.put("obj1",true); => exist map.get("obj1"); =>if null = not exist, else exist

  • 解决方案

    Performance:

    If you look at the source code of HashSet (at least JDK 6, 7 and 8), it uses HashMap internally, so it basically does exactly what you are doing with sample code.

    So, if you need a Set implementation, you use HashSet, if you need a Map - HashMap. Code using HashMap instead of HashSet will have exactly the same performance as using HashSet directly.

    Choosing the right collection

    Map - maps keys to values (associative array) - en.wikipedia/wiki/Associative_array.

    Set - a collection that contains no duplicate elements - en.wikipedia/wiki/Set_(computer_science).

    If the only thing you need your collection for is to check if an element is present in there - use Set. Your code will be cleaner and more understandable to others.

    If you need to store some data for your elements - use Map.

    更多推荐

    为什么HashMap比HashSet更快?

    本文发布于:2023-05-29 03:33:23,感谢您对本站的认可!
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:更快   HashMap   HashSet

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!