如何在集合中搜索(使用比较器)

发布于 2024-09-11 19:44:47 字数 533 浏览 2 评论 0原文

我想在 Set 中进行搜索,而不需要手动迭代元素,但似乎没有方法可以执行 Collections.search(myset, target, new ComparatorThing()) 。难道我没看到什么吗?

谢谢。

编辑:

  • 我正在寻找元素自然顺序之外的另一个字段。
  • 作为手动解决方法,我使用了以下静态方法。应该没问题,因为无论如何您都无法在比较器中使用自定义字段对对方做出任何假设。
public static  T search(final Set set, final T searchEntry, final Comparator comparator) {
    for (final T entry : set) {
        if (comparator.compare(entry, searchEntry) == 0) {
            return entry;
        }
    }

    return null;
}

I want to search in a Set without iterating manually over the elments but there does not seem to be a method to do Collections.search(myset, target, new ComparatorThing()). Am I not seeing something?

Thanks.

Edit:

  • I am searching for another field than the natural order of the elements.
  • As a manual workaround I used the following static method. Should okay, since you can't make any assumtions about the other using a custom field in the comparator anyways.
public static  T search(final Set set, final T searchEntry, final Comparator comparator) {
    for (final T entry : set) {
        if (comparator.compare(entry, searchEntry) == 0) {
            return entry;
        }
    }

    return null;
}

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

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

发布评论

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

评论(4

奈何桥上唱咆哮 2024-09-18 19:44:47

看一下 http://commons.apache.org/collections/ 其中提供了示例: public static java.util.Set SetUtils.predicatedSet(set, predicate)

Take a look at http://commons.apache.org/collections/ which provides for example: public static java.util.Set SetUtils.predicatedSet(set, predicate)

吃→可爱长大的 2024-09-18 19:44:47

这里需要更多详细信息 - 您是否尝试通过 Set 中包含的对象中的单个字段进行搜索?或者只是在 Set 中查找某个元素?

Set 本身作为裸接口的想法没有顺序的概念 - 您需要迭代每个元素。

但是,如果您将自己限制为 SortedSet,其中存在适当的排序,您可能会利用排序,但由于 Set 不允许随机访问,您仍然必须迭代每个元素,或者了解有关集合的更多信息,而不仅仅是它是一个 Set

您能详细说明一下您的算法以及您想要实现的目标吗?

Set 可能不是表示您想要“搜索”的数据的理想方式。

Need some more details here - are you attempting to search by an individual field in the Object contained in the Set? Or just find a certain element in the Set?

The idea of a Set itself, as the bare interface, has no idea of ordering - you would need to iterate over every element.

However if you restrict yourself to SortedSet, in which there is an ordering in place, you could possibly take advantage of the ordering, but since Sets do not allow for random access, you would still have to either iterate over every element or know more information about the collection beyond just that it's a Set.

Can you elaborate more on your algorithm and what you are trying to accomplish?

It is likely that a Set is not the ideal way to represent the data you want to "search" through.

婴鹅 2024-09-18 19:44:47

尝试从 Collection 接口中的 contains(Object o) 。 Set 接口扩展了 Collection,因此所有集合都需要实现 Collection 方法。

请记住,如果您只知道要搜索的对象是一个集合,那么您无法保证有任何方法可以在不迭代每个元素的情况下进行搜索,因为此 contains() 方法可能会也可能不会,具体取决于您实际使用的集合实现类型。

参考

Try contains(Object o), from the Collection interface. The Set interface extends Collection, so all sets are required to implement the Collection methods.

Bear in mind, if all you know of your object to search is that it is guaranteed to be a set, you have no guarantee there IS any way to search without iterating over each element, as this contains() method may or may not do depending on what type of set implementation you're actually using.

References

平定天下 2024-09-18 19:44:47

TreeSet 有一些可能有用的方法,例如 ceiling 用于搜索下一个大于或等于搜索键的元素,floor 用于获取下一个较低的元素。还有 headSet、tailSet 和 subSet 来搜索集合中更低、更大或给定限制之间的部分。

TreeSet has some methods that might be useful, for example ceiling to search for the next element greater or equals to the search key, floor to get the next lower element. Also headSet, tailSet and subSet to search for parts of a set lower, greater or between given limits.

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