How is it possible for Java HashMap to perform constant time lookup O(1) for “get” operations?

前端 未结 3 1736
不思量自难忘°
不思量自难忘° 2021-01-07 09:34

I understand the basics of how a HashMap works - hm.put(obj) finds the correct bucket to place the object into, based on the obj.hashCode value. Then within that bucket if

3条回答
  •  南方客
    南方客 (楼主)
    2021-01-07 09:49

    A hash table does not need to search every bucket, just like you don't need to search every shelf on the library, because you can look up it's location in the index cards, and you don't need to search every card in the index because they are sorted... (not sure if that helps as I'm not sure if people still go to libraries or if they still have index cards)

提交回复
热议问题