Firebase Performance of Hashmap versus TreeMap
This question already has an answer here:
Both classes, Hashmap and Treemap implement the Map
interface. Both classes offer mostly the same functionality but the most important difference between this classes is the order in which iteration through the entries will happen.
HashMap
offers no guarantees about the iteration order and it can also change completely when new elements are added/deleted. Lookup-array structure, based on hashCode(), equals() implementations, O(1) runtime complexity for inserting and searching, unsorted.
TreeMap
will iterate according to the natural ordering
of the containing keys according to their compareTo()
method. This can also be accomplished with an external Comparator. It also implements the SortedMap interface. Tree structure, based on compareTo() implementation, O(log(N)) runtime complexity for inserting and searching, sorted.
Use a HashMap
unless you have some need for ordering. HashMap is faster!
. But as you said that you mostly need your data "sorted by key", just use the TreeMap
.
上一篇: Hashset vs Treeset