Java HashMap 中的桶数是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18636576/
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
What is meant by number of buckets in the HashMap?
提问by Thinker
I was reading about Hashmap.
我正在阅读有关 Hashmap 的内容。
An instance of HashMap has two parameters that affect its performance: initial capacity and load factor. The capacity is the number of buckets in the hash table.
HashMap 的实例有两个影响其性能的参数:初始容量和负载因子。容量是哈希表中的桶数。
If there are 10 key value pairs in the Hashmap. Assume there Hashcode is different.
如果Hashmap中有10个键值对。假设有 Hashcode 是不同的。
Each will resides in one bucket right? Or one bucket can have bucket multiple key-value pairs?
每个将驻留在一个桶中,对吗?或者一个桶可以有多个键值对?
Since bucket
in english means a big thing where many objects can reside.
因为bucket
在英语中意味着可以容纳许多对象的大事。
采纳答案by vikingsteve
Yes, exactly, each bucket can have multiple key-value pairs.
是的,确切地说,每个桶可以有多个键值对。
The object's hashCode()
determines which bucket it goes into, via this expression: object.hashCode() % n
, where n = the total number of buckets and %
is the modulus operator.
对象hashCode()
通过以下表达式确定它进入哪个桶:object.hashCode() % n
,其中 n = 桶的总数并且%
是模运算符。
Most often the objects will be well distributed across buckets, but you have no guarantee where they go. This depends on the data and the hashCode function.
大多数情况下,对象会很好地分布在存储桶中,但您无法保证它们的去向。这取决于数据和 hashCode 函数。
Obviously, when the hashCode implementation is poor, the performance of the hashmap will go down.
显然,当hashCode 实现不佳时,hashmap 的性能就会下降。
Also read up on the equals / hashcode contract, which is relevant.
还阅读了相关的 equals/hashcode 合同。
回答by Narendra Pathai
Bucket with hashcode 1 Bucket with hashcode 2 and similar
K and V K and V
K and V K and V
So the hashCode()
of the key decides in which bucket the K V pair goes and the same hashCode
is used to find the K V pair while lookup.
因此hashCode()
,密钥的 决定了 KV 对在哪个桶中,同样hashCode
用于在查找时找到 KV 对。
hashCode()
should never return a constantvalue. As that would mean that all the objects will be in a single bucket. And that would be same as not using a Map
in the first place. As all the key value pairs would be in same bucket, Java will have to iterate through all the objects to find the key.
hashCode()
永远不应该返回一个常量值。因为这意味着所有对象都将在一个存储桶中。这Map
与首先不使用 a 相同。由于所有键值对都在同一个桶中,Java 必须遍历所有对象才能找到键。
回答by Balazs Varhegyi
In java if you store an object in HashMap first HashMap implementation calls the hashCode() method to find a bucket location. Then it stores both: the key and value as an Entry. NB! it stores the key also because it's crucial during retrieving the object. Two object can have the same hashcode so if this happens then HashMap will use the same bucket location and store the second object there too. Inside it uses a LinkedList for this. (not java.util.LinkedList, but a simpler implementation)
在 java 中,如果您在 HashMap 中存储一个对象,则首先 HashMap 实现调用 hashCode() 方法来查找存储桶位置。然后它同时存储:键和值作为条目。注意!它存储密钥也是因为它在检索对象期间至关重要。两个对象可以具有相同的哈希码,因此如果发生这种情况,HashMap 将使用相同的存储桶位置并将第二个对象也存储在那里。为此,它在内部使用了一个 LinkedList。(不是 java.util.LinkedList,而是一个更简单的实现)
During retrieving: you have a key -> hashCode() -> bucket location -> search in LinkedList by key -> returning object.
在检索期间:您有一个键 -> hashCode() -> 存储桶位置 -> 通过键在 LinkedList 中搜索 -> 返回对象。
EDIT: So you have one bucket on the same location but a bucket is a LinkedList so it can store multiple Entries. So the number of buckets is the capacity of Hashmap and describes how many Entries you can store without linking them in a list.
编辑: 所以您在同一位置有一个存储桶,但存储桶是一个 LinkedList,因此它可以存储多个条目。所以桶的数量就是Hashmap的容量,描述了你可以存储多少个条目,而无需将它们链接到列表中。
You can find a great article and more detailed explanation here: http://javahungry.blogspot.com/2013/08/hashing-how-hash-map-works-in-java-or.htmlhttp://javarevisited.blogspot.com/2011/02/how-hashmap-works-in-java.html
你可以找到一个伟大的文章,在这里更详细的解释: http://javahungry.blogspot.com/2013/08/hashing-how-hash-map-works-in-java-or.html HTTP://javarevisited.blogspot .com/2011/02/how-hashmap-works-in-java.html