简体   繁体   English

为什么HashMap比HashSet更快?

[英]Why is HashMap faster than HashSet?

I have been reading/researching the reason why HashMap is faster than HashSet . 我一直在阅读/研究HashMapHashSet更快的原因。

I am not quite understanding the following statements: 我不太了解以下陈述:

  1. HashMap is faster than HashSet because the values are associated to a unique key. HashMapHashSet更快,因为值与唯一键相关联。

  2. 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 ,使用密钥对象计算哈希码值。

  3. 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: 总结我的问题:

  1. I thought HashMap and HashSet calculate the hashcode in the same way. 我认为HashMapHashSet以相同的方式计算哈希码。 Why are they different? 他们为什么不同?

  2. Can you provide a concrete example how HashSet and HashMap calculating the hashcode differently? 你能提供一个具体的例子, HashSetHashMap如何以不同的方式计算哈希码?

  3. I know what a "key object" is, but what does it mean by "member object"? 我知道什么是“关键对象”,但“成员对象”是什么意思?

  4. 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.

相关问题 HashSet内部使用hashmap实现,那么为什么hashmap比hashset更快? - HashSet internally uses hashmap for its implementation, then why is hashmap faster than hashset? 为什么ArrayList比java中的HashSet更快? - Why ArrayList is faster than HashSet in java? 为什么创建HashMap比创建Object []更快? - Why is creating a HashMap faster than creating an Object[]? 为什么哈希集的性能比列表更快? - Why the hashset's performance is way faster than list? 为什么Collections.synchronizedSet(HashSet)比HashSet对addAll,retainAll和contains更快? - Why is Collections.synchronizedSet(HashSet) faster than HashSet for addAll, retainAll, and contains? Hashtable 比 HashMap 快? - Hashtable is faster than HashMap? 为什么在LinkedHashMap中迭代桶的速度比HashMap快? - Why iteration through buckets in LinkedHashMap is faster than HashMap? 为什么从 HashSet 中删除元素的代码比添加到 HashMap 需要更长的时间? - Why code removing elements from HashSet Takes much longer than adding to HashMap? 为什么HashSet的内部实现会创建虚拟对象以在HashMap中作为值插入而不是插入空值? - Why the internal implementation of HashSet creates dummy objects to insert as values in HashMap rather than inserting nulls? 什么时候使用 TreeSet 比 HashSet 更快? - When is using a TreeSet faster than a HashSet?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM