What is the difference between `HashMap`’s `get()` time complexity and worst-case time complexity, and what causes worst-case?

Java Associate Hard

Java Associate — Hard

What is the difference between `HashMap`’s `get()` time complexity and worst-case time complexity, and what causes worst-case?

Key points

  • Hash collisions can cause worst-case time complexity in `HashMap`
  • O(1) average time complexity is maintained for most lookups
  • Java 8 introduced tree bins to improve worst-case to O(log n)
  • Worst-case scenario involves linked list traversal in the same bucket

Ready to go further?

Related questions