HashMap get/put complexity

前端 未结 6 661
甜味超标
甜味超标 2020-11-22 11:50

We are used to saying that HashMap get/put operations are O(1). However it depends on the hash implementation. The default object hash is actually

6条回答
  •  情话喂你
    2020-11-22 12:37

    HashMap operation is dependent factor of hashCode implementation. For the ideal scenario lets say the good hash implementation which provide unique hash code for every object (No hash collision) then the best, worst and average case scenario would be O(1). Let's consider a scenario where a bad implementation of hashCode always returns 1 or such hash which has hash collision. In this case the time complexity would be O(n).

    Now coming to the second part of the question about memory, then yes memory constraint would be taken care by JVM.

提交回复
热议问题