Which data structure should be used in which situation!
- Unless there is a special reason, use
HashMapwhich has goodsearch performance - If you want to
guarantee order, use LinkedHashMap - If you want to
iterateover key values in a consistent manner, useTreeMap
- TreeMap is a data structure similar to HashMap, but the difference is that TreeMap's data is sorted
- Since HashMap's data is not sorted, it may be slower than TreeMap for data searching
- However, HashMap uses less memory than TreeMap
HashMapdoes not guarantee orderLinkedHashMapguarantees the order of insertion!
TreeMapguarantees order using the comparison operation of the class used as the Key value- It automatically
sortsbased on the key value
- It automatically
- The time complexity of
HashMapisO(1)- Because it uses
hash values
- Because it uses
- The time complexity of
TreeMapisO(log n)- In return, you can get a
sorted order
- In return, you can get a
HashMapallowsnull as a keyTreeMapdoesnot allownull as a key
- When you need to store and search ordered data
- It has synchronization handling, making it
Thread-safe
- It has synchronization handling, making it
- When you need to search data in sorted order
- When insertion, deletion, and search operations on data are frequently performed
- When the data size is very large
- When insertion, deletion, and search operations on data are not frequently performed