查找()和字典之间的差异(名单())

编程入门 行业动态 更新时间:2024-10-28 01:26:46
本文介绍了查找()和字典之间的差异(名单())的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想换我的头周围的数据结构是最有效和何时/何使用哪些。

I'm trying to wrap my head around which data structures are the most efficient and when / where to use which ones.

现在,这可能是因为我根本就不懂结构不够好,但如何从一个 ILookup(键,...)不同词典(键,列表(...的)的)?

Now, it could be that I simply just don't understand the structures well enough, but how is an ILookup(of key, ...) different from a Dictionary(of key, list(of ...))?

也将在那里我想使用 ILookup 和地点会是在程序速度/内存/数据访问等?

Also where would I want to use an ILookup and where would it be more efficient in terms of program speed / memory / data accessing, etc?

推荐答案

二显著差异:

  • 查找是不可改变的。耶:)(至少,我认为具体的查找类是不可变的,而 ILookup 接口没有提供任何变异的成员,有可以的是其他可变实现的,当然。)
  • 当你查找一个关键这是不是在查找present,你会得到一个空的顺序背,而不是 KeyNotFoundException 。 (因此没有 TryGetValue ,AFAICR。)
  • Lookup is immutable. Yay :) (At least, I believe the concrete Lookup class is immutable, and the ILookup interface doesn't provide any mutating members. There could be other mutable implementations, of course.)
  • When you lookup a key which isn't present in a lookup, you get an empty sequence back instead of a KeyNotFoundException. (Hence there's no TryGetValue, AFAICR.)

他们可能是在效率等同 - 查找可能使用词典< TKEY的,GroupingImplementation< TValue>> 的场景,例如落后。根据您的要求二者之间做出选择。我个人而言,查找通常是一个比一个更适合词典< TKEY的,列表与LT; TValue>> ,主要是由于上面的第一个两分。

They're likely to be equivalent in efficiency - the lookup may well use a Dictionary<TKey, GroupingImplementation<TValue>> behind the scenes, for example. Choose between them based on your requirements. Personally I find that the lookup is usually a better fit than a Dictionary<TKey, List<TValue>>, mostly due to the first two points above.

请注意,作为一个实现细节,具体落实 IGrouping&LT;,&GT; 这是用于数值工具的IList&LT; TValue&GT; ,这意味着它的效率与 COUNT(),的ElementAt()等。

Note that as an implementation detail, the concrete implementation of IGrouping<,> which is used for the values implements IList<TValue>, which means that it's efficient to use with Count(), ElementAt() etc.

更多推荐

查找()和字典之间的差异(名单())

本文发布于:2023-10-19 19:06:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1508484.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字典   差异   名单

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!