[英]Why is HashMap faster than HashSet?
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 than HashSet
because the values are associated to a unique key. HashMap
比HashSet
更快,因为值与唯一键相关联。
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. 在
HashSet
,成员对象用于计算哈希码值,对于两个对象,哈希码值可以相同,因此使用equals()
方法检查是否相等。 If it returns false
, that means the two objects are different. 如果返回
false
,则表示两个对象不同。 In HashMap
, the hashcode value is calculated using the key object. 在
HashMap
,使用密钥对象计算哈希码值。
The HashMap
hashcode value is calculated using the key object. 使用密钥对象计算
HashMap
哈希码值。 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. 这里,成员对象用于计算哈希码,对于两个对象可以是相同的,因此使用
equals()
方法来检查相等性。 If it returns false
, that means the two objects are different. 如果返回
false
,则表示两个对象不同。
To conclude my question: 总结我的问题:
I thought HashMap
and HashSet
calculate the hashcode in the same way. 我认为
HashMap
和HashSet
以相同的方式计算哈希码。 Why are they different? 他们为什么不同?
Can you provide a concrete example how HashSet
and HashMap
calculating the hashcode differently? 你能提供一个具体的例子,
HashSet
和HashMap
如何以不同的方式计算哈希码?
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. HashMap
可以做与HashSet
相同的事情,并且速度更快。 Why do we need HashSet
? 为什么我们需要
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. 如果你看一下HashSet的源代码(至少是JDK 6,7和8),它会在内部使用HashMap,所以它基本上就像你在使用示例代码一样。
So, if you need a Set implementation, you use HashSet, if you need a Map - HashMap. 因此,如果需要Set实现,则使用HashSet,如果需要Map - HashMap。 Code using HashMap instead of HashSet will have exactly the same performance as using HashSet directly.
使用HashMap而不是HashSet的代码与直接使用HashSet具有完全相同的性能。
Choosing the right collection 选择合适的系列
Map - maps keys to values (associative array) - http://en.wikipedia.org/wiki/Associative_array . Map - 将键映射到值(关联数组) - http://en.wikipedia.org/wiki/Associative_array 。
Set - a collection that contains no duplicate elements - http://en.wikipedia.org/wiki/Set_(computer_science) . Set - 不包含重复元素的集合 - 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. 如果您需要收集的唯一内容是检查其中是否存在元素 - 请使用Set。 Your code will be cleaner and more understandable to others.
您的代码将更清晰,更易于理解。
If you need to store some data for your elements - use Map. 如果您需要为元素存储一些数据 - 请使用Map。
None of these answers really explain why HashMap is faster than HashSet. 这些答案都没有真正解释为什么 HashMap比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.
它们都必须计算哈希码,但要考虑HashMap的键的性质 - 它通常是一个简单的字符串,甚至是一个数字。 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.
如果HashMap的键与存储在HashSet中的键相同,那么性能就没有真正的区别。 The difference comes in the what sort of object is the HashMap's key.
不同之处在于HashMap的关键是什么类型的对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.