Java 为什么 HashMap 比 HashSet 快?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16278995/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why is HashMap faster than HashSet?
提问by runcode
I have been reading/researching the reason why HashMap
is faster than HashSet
.
我一直在阅读/研究为什么HashMap
比HashSet
.
I am not quite understanding the following statements:
我不太理解以下陈述:
HashMap
is faster thanHashSet
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 soequals()
method is used to check for equality. If it returnsfalse
, that means the two objects are different. InHashMap
, 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, soequals()
method is used to check for equality. If it returnsfalse
, that means the two objects are different.
HashMap
比HashSet
因为值与唯一键相关联更快。在 中
HashSet
,成员对象用于计算两个对象可以相同的哈希码值,因此equals()
方法用于检查相等性。如果返回false
,则意味着两个对象不同。在 中HashMap
,哈希码值是使用键对象计算的。的
HashMap
哈希码值使用的密钥对象进行计算。这里使用成员对象来计算hashcode,两个对象可以相同,所以使用equals()
method来检查是否相等。如果返回false
,则意味着两个对象不同。
To conclude my question:
总结我的问题:
I thought
HashMap
andHashSet
calculate the hashcode in the same way. Why are they different?Can you provide a concrete example how
HashSet
andHashMap
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 asHashSet
, and faster. Why do we needHashSet
? Example:HashMap <Object1, Boolean>= new HashMap<Object1, boolean>(); map.put("obj1",true); => exist map.get("obj1"); =>if null = not exist, else exist
我以同样的方式思考
HashMap
和HashSet
计算哈希码。他们为什么不同?你能提供一个具体的例子,如何
HashSet
与HashMap
不同的计算哈希码?我知道什么是“关键对象”,但“成员对象”是什么意思?
HashMap
可以做同样的事情HashSet
,而且速度更快。我们为什么需要HashSet
?例子:HashMap <Object1, Boolean>= new HashMap<Object1, boolean>(); map.put("obj1",true); => exist map.get("obj1"); =>if null = not exist, else exist
回答by denis
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.
如果您查看 HashSet 的源代码(至少是 JDK 6、7 和 8),它在内部使用了 HashMap,因此它基本上完全符合您对示例代码所做的工作。
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.
因此,如果您需要 Set 实现,则使用 HashSet,如果您需要 Map - HashMap。使用 HashMap 而不是 HashSet 的代码将与直接使用 HashSet 具有完全相同的性能。
Choosing the right collection
选择正确的收藏
Map - maps keys to values (associative array) - http://en.wikipedia.org/wiki/Associative_array.
映射 - 将键映射到值(关联数组)- http://en.wikipedia.org/wiki/Associative_array。
Set - a collection that contains no duplicate elements - http://en.wikipedia.org/wiki/Set_(computer_science).
集合 - 不包含重复元素的集合 - http://en.wikipedia.org/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.
如果您需要收集的唯一内容是检查其中是否存在元素 - 使用 Set。你的代码会更清晰,更容易被其他人理解。
If you need to store some data for your elements - use Map.
如果您需要为元素存储一些数据 - 使用 Map。
回答by TwinFeats
None of these answers really explain whyHashMap is faster than HashSet. They both have to calculate the hashcode, but think about the nature of the key of a HashMap - it is typically a simple String or even a number. Calculating the hashcode of that is much faster than the default hashcode calculation of an entire object. If the key of the HashMap was the same object as that stored in a HashSet, there would be no real difference in performance. The difference comes in the what sort of object is the HashMap's key.
这些答案都没有真正解释为什么HashMap 比 HashSet 快。他们都必须计算哈希码,但考虑一下 HashMap 键的性质——它通常是一个简单的字符串,甚至是一个数字。计算它的哈希码比整个对象的默认哈希码计算快得多。如果 HashMap 的键与存储在 HashSet 中的对象相同,则性能不会有真正的差异。不同之处在于 HashMap 的键是哪种对象。