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

查看:1699
本文介绍了什么是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.

每个都会驻留在一个桶中?
或一个桶可以有桶多个键值对?

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

由于

推荐答案

是的,每个桶都可以有多个键值对。

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天全站免登陆