开发者

Is there any thing hashmap can do but map cannot?

I only know that the difference between hashmap and map is that hashmap is implemented with hash function but map is implemente开发者_Python百科d with tree. Could any body add anything more?

Based on this, is there any thing hashmap can do but map cannot?


  • Hashmaps have average case better performance for access (O(1)), but worse worst case performance (O(n)). Maps are always O(lg(n)).

  • Maps are ordered by their key, hashmaps are not.

  • Hashmaps generally use more memory than maps.

  • Maps typically allow for faster iteration.

  • Good hash functions are harder to write than good ordering functions (and more difficult to analyse).

I don't believe there's anything that a hashmap can do that a map can't.


A map requires the key has a strict weak ordering, which perhaps may not exist. A hashmap only needs a hash function. So in this way a hashmap can be used with keys that have no strict weak ordering.


One advantage that hashmaps have over trees is that, in a multi-threading environment, you don't have to lock the whole container to add or remove a single key - locking the single relevant entry in the hash table is (almost) enough.

Almost because there may be metadata (number of items in the hashtable, for instance) to update. And you probably need to lock the whole table to grow or shrink it, of course.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜