top | item 30237631

(no title)

guiand | 4 years ago

Hash tables are (usually) faster to do all sorts of operations than tree based maps, as most operations become a simple function to calculate a tree’s hash followed by a table lookup. Of course, they’re unordered, so if you need to iterate in order, or find all keys < a certain value, or things like that, tree maps can be better for your algorithm.

Also, TreeMap uses a red-black tree to implement the map, which is a basic type of binary tree. Depending on the data you’d like to store, other kinds of tree-based maps can have better performance characteristics. A map based on a Splay Tree[1] speeds up repeated accesses, so it could perform well if you had keys that were cheap to compute an ordering but expensive to compute a hash, and your access pattern has good temporal locality.

[1] https://en.wikipedia.org/wiki/Splay_tree

discuss

order

No comments yet.