在 HashSet 中搜索字符串数组中的任意元素

发布于 2024-10-15 05:27:23 字数 332 浏览 1 评论 0原文

我有一个字符串哈希集和一个字符串数组。我想找出数组中的任何元素是否存在于 HashSet 中。我有以下代码可以工作,但我觉得它可以做得更快。

public static boolean check(HashSet<String> group, String elements[]){
    for(int i = 0; i < elements.length; i++){
        if(group.contains(elements[i]))
            return true;
    }
    return false;
}

谢谢。

I have a HashSet of strings and an array of strings. I want to find out if any of the elements in the array exists in the HashSet. I have the following code that work, but I feel that it could be done faster.

public static boolean check(HashSet<String> group, String elements[]){
    for(int i = 0; i < elements.length; i++){
        if(group.contains(elements[i]))
            return true;
    }
    return false;
}

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

热情消退 2024-10-22 05:27:23

在这种情况下(使用数组),它是O(n),速度再快不过了。

如果你只是想让代码更简洁:

 return !Collections.disjoint(group, Arrays.asList(elements));

It's O(n) in this case (array is used), it cannot be faster.

If you just want to make the code cleaner:

 return !Collections.disjoint(group, Arrays.asList(elements));
心欲静而疯不止 2024-10-22 05:27:23

这似乎有些道理。 HashSet 的 O(1)(通常) contains() 因为它只需对您提供的字符串进行散列即可找到索引,并且那里要么有东西,要么没有。

如果您需要检查数组中的每个元素,那么根本没有任何更快的方法来执行此操作(当然,按顺序)。

That seems somewhat reasonable. HashSet has an O(1) (usually) contains() since it simply has to hash the string you give it to find an index, and there is either something there or there isn't.

If you need to check each element in your array, there simply isn't any faster way to do it (sequentially, of course).

假装不在乎 2024-10-22 05:27:23

...但我觉得可以做得更快。

我不认为有更快的方法。您的代码平均为 O(N),其中 N 是数组中字符串的数量。我不认为你可以改进这一点。

... but I feel that it could be done faster.

I don't think there is a faster way. Your code is O(N) on average, where N is the number of strings in the array. I don't think that you can improve on that.

南…巷孤猫 2024-10-22 05:27:23

正如其他人所说,算法最慢的部分是迭代数组的每个元素。可以让它更快的唯一方法是,如果您事先知道有关数组内容的一些信息,这允许您跳过某些元素,例如数组是否已排序并且在已知位置有重复项或其他元素。如果输入本质上是随机的,那么您无能为力。

As others have said, the slowest part of the algorithm is iterating over every element of the array. The only way you could make it faster would be if you knew some information about the contents of the array beforehand which allowed you to skip over certain elements, like if the array was sorted and had duplicates in known positions or something. If the input is essentially random, then there's not a lot you can do.

将军与妓 2024-10-22 05:27:23

如果您知道该集合是有序集合,并且该数组已排序,则可以获取 从开始到结束的间隔设置可能比 O(|array| * access-time(set)) 更好,并且这特别允许一些比 O(|array|) 更好的负结果,但如果你进行散列,则不能。

If you know that the set is a sorted set, and that the array is sorted, you can get the interval set from the start to the end to possibly do better than O(|array| * access-time(set)), and which especially allows for some better than O(|array|) negative results, but if you're hashing you can't.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文