Difference between HashMap and TreeMap

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

Author: Maniero, 2018-11-08

1 answers

HashMap it is a data structure based on scattering through a calculation function hash of 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 Access form is always very fast and can in most cases have access time complexity any constant (O(1)). Can not have duplicate keys and does not maintain a specific order, it is considered unordered.

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

See more at best performace for few hits: HashMap or TreeMap?.

 11
Author: Maniero, 2020-08-14 12:06:05