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.
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.
Obviously, when the hashCode implementation is poor, the performance of the hashmap will go down.
Also read up on the equals / hashcode contract, which is relevant.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…