哈希表的意义是什么? [英] What's the point of a hash table?

查看:170
本文介绍了哈希表的意义是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有使用动态语言在数组/字典之外的哈希表的经验,因此我最近发现,在内部它们是通过对键进行哈希并使用其存储值来实现的.我不明白的是为什么不将键(字符串,数字等)作为键存储值,而不是对其进行散列并存储.

I don't have experience with hash tables outside of arrays/dictionaries in dynamic languages, so I recently found out that internally they're implemented by making a hash of the key and using that to store the value. What I don't understand is why aren't the values stored with the key (string, number, whatever) as the, well, key, instead of making a hash of it and storing that.

推荐答案

这几乎是重复的:长话短说,您可以检查密钥是否已经非常快速地存储,并且同样可以快速地存储新的映射.否则,您必须保留一个排序的键列表,这对于存储和检索映射要慢得多.

Long story short, you can check if a key is already stored VERY quickly, and equally rapidly store a new mapping. Otherwise you'd have to keep a sorted list of keys, which is much slower to store and retrieve mappings from.

这篇关于哈希表的意义是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆