LINQ 中用于迭代 HashSet的巧妙替代方案是:
我在 HashSet
内有一个正在使用的 URL 白名单。我正在尝试查找 url
是否以白名单中的任何项目开头(必须是这样)。
编辑:前面的示例有点误导,并且有一个拼写错误 - 我已经有一个像 yahoo.com 这样的基本 URL,白名单只是路径。
HashSet<string> whiteList = new HashSet<string>();
string path = "/sport/baseball/";
bool validUrl = false;
foreach (string item in whiteList)
{
if (path.StartsWith(item))
{
validUrl = true;
break;
}
}
是否有更优雅的方式使用 LINQ(到对象)进行此查找?该列表并不大,因此性能不是问题。
I've got a whitelist of URLs I'm using, inside a HashSet<string>
. I'm trying to find if the url
starts with any of the items in the white list (it has to be that way round).
Edit: The previous example was a bit misleading and had a typo - I already have a base url like yahoo.com, the whitelist is just the path.
HashSet<string> whiteList = new HashSet<string>();
string path = "/sport/baseball/";
bool validUrl = false;
foreach (string item in whiteList)
{
if (path.StartsWith(item))
{
validUrl = true;
break;
}
}
Is there a more elegant way of doing this lookup with LINQ (to objects)? The list isn't huge so performance isn't an issue.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
顺便说一句,一般来说,哈希表对于此类问题(您没有密钥并且基于函数匹配密钥)来说并不是很好的数据结构,因为您必须枚举整个表的所有时间。您可以使用简单的
List
来保存项目,这样您将获得更好的性能。By the way, in general, hash tables are not good data structures for these kind of problems (where you don't have the key and are matching the key based on a function) as you'll have to enumerate the whole table all the time. You can use a simple
List<string>
to hold the items instead and you'll get better performance.这里的问题在于查找。白名单里有规律吗?即它是否始终是您所追求的域,而不一定是其中的页面或特定子域?
如果是这样,您可以使用 string.split 从字符串中获取第一个 URL 部分,然后使用哈希集的 .Contains() 方法来获取该项目。这将删除为列表中的每个元素运行一次的 string.StartsWith() 命令以及昂贵的字符串比较,并将其替换为一次性 string.split 和哈希集的 O(1) 查找。
The issue here is with the lookup. Do you have any regularity in the whitelist? i.e will it always be a domain you're after, not neccessarily the pages within or a specific subdomain?
If so you could use a string.split to grab the first URL part from your string, then use the .Contains() method of your hashset to get the item. This would remove the string.StartsWith() command which is run once for every element in the list, and an expensive string compare, and replace it with a one off string.split and a O(1) lookup of your hashset.