Sunday 8 June 2014

HashMap Vs TreeMap

1. TreeMap doesn't support null keys, Where as HashMap supports.

2. HashMaps perform lookup operation in O(1) time where as TreeMaps perform in O(log n) time.

3. TreeMap is an example of a SortedMap, which means that the order of the keys can be sorted, and when iterating over the keys, you can expect that they will be in order.

HashMap on the other hand, makes no such guarantee. Therefore, when iterating over the keys of a HashMap, you can't be sure what order they will be in.

4. You almost always use HashMap, you should only use TreeMap if you need your keys to be in a specific order.
 

Prevoius                                                 Next                                                 Home

No comments:

Post a Comment