Do keys with different hashes also get mapped to the same index in HashMap?

前端 未结 3 1599
深忆病人
深忆病人 2021-01-13 02:43

Looking at the code specifically line 393, it looks like different hashes have been mapped to same index. I had an understanding that the hashcode is used to determine what

3条回答
  •  北海茫月
    2021-01-13 03:30

    This check is an optimization taking into account for collisions.
    You can have 2 elements that have the same hash key (due to collision) and as a result are mapped to the same bucket. Same key different actual elements.
    So if e.hash != hash you don't need to check for equality (which could be an expensive operation)

提交回复
热议问题