What are the main differences between HashMap
and TreeMap
? In relation to the use, in which situations is the use of each one recommended?
What are the main differences between HashMap
and TreeMap
? In relation to the use, in which situations is the use of each one recommended?
HashMap
is a spreadsheet-based data structure through a hash calculation function 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 form of access is always very fast and can, in most cases, have any constant access time complexity (O (1)). It can not have duplicate keys and does not maintain a specific order, it is considered not ordered.
TreeMap
is a collection of data similar to % with% of where access to the elements has logarithmic access time complexity (O (logN)), which is very close to constant time even in large volumes, and unlike HashMap
, does not run the risk of the worst cases be very slow. Allows duplicate keys and maintains a sort of keys. If you need at least one of these two characteristics you should choose it.
See more in Best Performances for Few HashMap or TreeMap? .