HashMap 中的桶数是什么意思? [英] What is meant by number of buckets in the HashMap?

查看:60
本文介绍了HashMap 中的桶数是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读 Hashmap.

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中有10个键值对.假设 Hashcode 不同.

If there are 10 key value pairs in the Hashmap. Assume there Hashcode is different.

每个将驻留在一个桶中,对吗?或者一个bucket可以有bucket多个key-value对?

Each will resides in one bucket right? Or one bucket can have bucket multiple key-value pairs?

因为bucket在英文中的意思是一个可以容纳许多对象的大东西.

Since bucket in english means a big thing where many objects can reside.

推荐答案

没错,每个bucket可以有多个key-value对.

Yes, exactly, each bucket can have multiple key-value pairs.

对象的 hashCode() 确定它进入哪个桶,通过以下表达式:object.hashCode() % n,其中 n = 桶的总数,% 是取模运算符.

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 函数.

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实现不佳时,hashmap的性能会下降.

Obviously, when the hashCode implementation is poor, the performance of the hashmap will go down.

还阅读了相关的 equals/hashcode 合约.

Also read up on the equals / hashcode contract, which is relevant.

这篇关于HashMap 中的桶数是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆