LINQ 中用于迭代 HashSet的巧妙替代方案是:

发布于 2024-08-17 05:09:48 字数 515 浏览 3 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

那支青花 2024-08-24 05:09:49
bool validUrl = whiteList.Any(item => linkUrl.StartsWith(item));

顺便说一句,一般来说,哈希表对于此类问题(您没有密钥并且基于函数匹配密钥)来说并不是很好的数据结构,因为您必须枚举整个表的所有时间。您可以使用简单的 List 来保存项目,这样您将获得更好的性能。

bool validUrl = whiteList.Any(item => linkUrl.StartsWith(item));

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.

番薯 2024-08-24 05:09:49

这里的问题在于查找。白名单里有规律吗?即它是否始终是您所追求的域,而不一定是其中的页面或特定子域?

如果是这样,您可以使用 string.split 从字符串中获取第一个 URL 部分,然后使用哈希集的 .Contains() 方法来获取该项目。这将删除为列表中的每个元素运行一次的 string.StartsWith() 命令以及昂贵的字符串比较,并将其替换为一次性 string.split 和哈希集的 O(1) 查找。

HashSet<string> whiteList = new HashSet<string>();
//add items

string urlStartsWith = "http://www.yahoo.com";
bool validURL = whiteList.Contains(url);

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.

HashSet<string> whiteList = new HashSet<string>();
//add items

string urlStartsWith = "http://www.yahoo.com";
bool validURL = whiteList.Contains(url);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文