Difference between Hashmap and Treemap

Asked

Viewed 725 times

8

What are the main differences between HashMap and TreeMap? Regarding use, in which situations it is recommended to use each one?

1 answer

11


HashMap is a data structure based on scattering through a calculation function hash a key information of the object to be placed in the data collection. You always have a key that is calculated and a value associated with it. Access is always done by the key. The form of access is always very fast and can, in most cases, have any constant access time complexity (O(1)). Cannot have duplicate keys and does not maintain a specific order, is considered unordered.

TreeMap is a collection of data similar to HashMap where access to the elements has logarithmic access time complexity (O(log n)), which is very close to constant time even in large volumes, and unlike the HashMap, there is no risk that the worst cases will be very slow. It allows duplicate keys and maintains a classification of keys. If you need at least one of these two characteristics you should choose it.

See more in Best performance for few accesses: Hashmap or Treemap?.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.