快速字符串查找的最佳集合 [英] Best Collection for Fast String Lookup

查看:94
本文介绍了快速字符串查找的最佳集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个字符串列表以及一种快速确定该列表中是否包含字符串的方法。

I need a list of strings and a way to quickly determine if a string is contained within that list.

为了提高查找速度,我考虑了 SortedList Dictionary ;但是,当我只需要一个 string 时,它们都可以与 KeyValuePair 一起使用。

To enhance lookup speed, I considered SortedList and Dictionary; however, both work with KeyValuePairs when all I need is a single string.

我知道我可以使用 KeyValuePair ,而只是忽略 Value 部分。但是我确实希望提高效率,并且只是想知道是否有一个更适合我的需求的集合。

I know I could use a KeyValuePair and simply ignore the Value portion. But I do prefer to be efficient and am just wondering if there is a collection better suited to my requirements.

推荐答案

如果在.NET 3.5或更高版本上,请使用 HashSet< String>

If you're on .NET 3.5 or higher, use HashSet<String>.

否则, Dictionary< string,byte> (或者您想要用于 TValue 类型参数的任何类型)会比 SortedList 快得多个条目-后者将使用二进制搜索,因此将是O(log n)查找,而不是O(1)。

Failing that, a Dictionary<string, byte> (or whatever type you want for the TValue type parameter) would be faster than a SortedList if you have a lot of entries - the latter will use a binary search, so it'll be O(log n) lookup, instead of O(1).

这篇关于快速字符串查找的最佳集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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