如何在集合中搜索(使用比较器)
我想在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
看一下 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)
这里需要更多详细信息 - 您是否尝试通过
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 theSet
?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 sinceSet
s 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 aSet
.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.尝试从
Collection
接口中的contains(Object o)
。 Set 接口扩展了 Collection,因此所有集合都需要实现 Collection 方法。请记住,如果您只知道要搜索的对象是一个集合,那么您无法保证有任何方法可以在不迭代每个元素的情况下进行搜索,因为此
contains()
方法可能会也可能不会,具体取决于您实际使用的集合实现类型。参考
Try
contains(Object o)
, from theCollection
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
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.