对对象使用自定义通用集合比列表更快
我正在迭代 List
来查找匹配的元素。问题是该对象只有 2 个有效值,Name
和 Link
(都是字符串),但还有一些我不想比较的其他值。
我正在考虑使用 .NET 3.5 中的 HashSet
(这正是我正在寻找的东西 - 快速),但目标框架必须是 2.0。这里有一个叫做 Power Collections 的东西:http://powercollections.codeplex.com/,我应该使用它吗?
但也许还有其他方法吗?如果没有,您能给我推荐一个合适的定制系列吗?
I'm iterating through a List<>
to find a matching element. The problem is that object has only 2 significant values, Name
and Link
(both strings), but has some other values which I don't want to compare.
I'm thinking about using something like HashSet
(which is exactly what I'm searching for -- fast) from .NET 3.5 but target framework has to be 2.0. There is something called Power Collections here: http://powercollections.codeplex.com/, should I use that?
But maybe there is other way? If not, can you suggest me a suitable custom collection?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 .NET 2.0 中,您可以使用
Dictionary
代替HashSet
。Dictionary
使用哈希码来执行键查找,因此它具有与HashSet
类似的性能。至少有两种方法:第二种方法与使用 HashSet(如果可用)的方式非常相似。
In .NET 2.0 instead of a
HashSet<T>
you can use aDictionary<K, V>
.Dictionary
uses the hash code to perform key lookups so it has similar performace to theHashSet
. There are at least two approaches:The second method is very similar to how you would use a HashSet if it were available.
怎么样:
自定义类/集合将保存对象列表和两个字典,一个用于名称,一个用于链接。它们都有一个 int 值,它将作为对象的索引。我认为在这种情况下,我只需要检查名称字典中是否存在等于链接字典 int 的 int 值。
这是一个好方法吗?
How about this:
Custom class/collection wich will held List of objects and two dictionaries, one for the name and one for the link. Both of them will have a int value wich will be the index of object. I think that in that case I will only need to check if there is such int value of name dictionary that equals link dictionary int.
Is this a good approach?