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

前端 未结 3 1742
不思量自难忘°
不思量自难忘° 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 10:03

    Think of it this way: when you call get(key), the hashcode of the key is calculated and from this the one bucket among hundreds is pointed to in a single (set of) operation(s), i.e. you don't have to search through all 100 to arrive at the right bucket (in which case that would be a linear operation)

提交回复
热议问题