简体   繁体   English

列表与字典(Hashtable)

[英]List vs Dictionary (Hashtable)

This may be a silly question but I am reading about that Hashtables and Dictionaries are faster than a list because they index the items with keys. 这可能是一个愚蠢的问题,但我读到HashtablesDictionaries比列表更快,因为他们用键索引项目。

I know a List or Array is for elements without values, and a Dictionary is for elements with values. 我知道ListArray是针对没有值的元素,而Dictionary是针对具有值的元素。 So I would think that it maybe be smart to have a Dictionary with the value that you need as a key and the value equal in all of them? 因此,我认为拥有一个具有您需要的值作为键的Dictionary并且所有值都相等的Dictionary可能是明智的吗?

Update : 更新

Based on the comments what I think I need is a HashSet . 基于评论,我认为我需要的是HashSet This question talks about their performance. 这个问题谈到了他们的表现。

There are some weaknesses to Dictionary/Hashtable vs a List/array as well: Dictionary / Hashtable与List /数组之间也存在一些缺点:

  • You have to compute the hash value of the object with each lookup. 您必须使用每次查找计算对象的哈希值。
  • For small collections, iterating through the array can be faster than computing that hash, especially because a hash is not guaranteed to be unique 1 . 对于小型集合,迭代数组可能比计算该散列更快,特别是因为散列不能保证是唯一的1
  • They are not as good at iterating over the list of items. 他们不擅长迭代项目列表。
  • They are not very good at storing duplicate entries (sometimes you legitimately want a value to show in an array more than once) 他们不擅长存储重复的条目(有时你合法地想要一个值在一个数组中多次显示)
  • Sometimes a type does not have a good key to associate with it 有时类型没有与之关联的好键

Use what fits the situation. 使用适合的情况。 Sometimes that will be a list or an array. 有时这将是一个列表或一个数组。 Sometimes it will be a Dictionary. 有时它会是一个字典。 You should almost never use a HashTable any more (prefer Dictionary<KeyType, Object> if you really don't what type you're storing). 你几乎不应该再使用HashTable了(如果你真的不存储你正在存储的类型,则更喜欢Dictionary <KeyType,Object>)。

1 It usually is unique, but because there is a small potential for collisions the collection must check the bucket after computing the hash value. 1它通常是唯一的,但由于冲突的可能性很小,因此集合必须在计算哈希值后检查存储桶。

"Faster" depends on what you need them for. “更快”取决于你需要它们。

A .NET List is just a slab of continuous memory (this in not a linked list), which makes it extremely efficient to access sequentially (especially when you consider the effects of caching and prefetching of modern CPUs) or "randomly" trough a known integer index. .NET List只是连续内存的一块(这不是链接列表),这使得顺序访问非常有效(特别是当您考虑缓存和预取现代CPU的影响时)或“随机”通过已知的整数索引。 Searching or inserting elements (especially in the middle) - not so much. 搜索或插入元素(特别是在中间) - 不是那么多。

Dictionary is an associative data structure - a key can be anything hashable (not just integer index), but elements are not sorted in a "meaningful" way and the access through the known key is not as fast as List 's integer index. Dictionary是一种关联数据结构 - 一个键可以是任何可哈希的(不仅仅是整数索引),但是元素不是以“有意义”的方式排序,而通过已知键的访问不如List的整数索引那么快。

So, pick the right tool for the job. 因此,为工作选择合适的工具。

Your statement "list or array is for elements without values, and dictionary is for elements with values", is not strictly true. 您的语句“列表或数组用于没有值的元素,而字典用于具有值的元素”,并非严格来说。

More accurately, a List is a collection of elements, and a Hashtable or Dictionary is a collection of elements along with a unique key to be used to access each one. 更准确地说,List是元素的集合,Hashtable或Dictionary是元素的集合以及用于访问每个元素的唯一键。

Use a list for collections of a very few elements, or when you will only need to access the entire collection, not a single element of the collection. 将列表用于极少数元素的集合,或者只需要访问整个集合,而不是集合中的单个元素。

Use a Hashtable or Dictionary when the collection is large and/or when you will need to find/access individual members of the collection. 当集合很大时和/或当您需要查找/访问集合的各个成员时,请使用Hashtable或Dictionary。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM