java 如何在集合中搜索(使用比较器)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3363626/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to search in a Set (using a Comparator)
提问by Benjamin Peter
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?
我想在 Set 中搜索而不手动遍历元素,但似乎没有方法可以执行 Collections.search(myset, target, new ComparatorThing())。我没有看到什么吗?
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;
}
采纳答案by David Soroko
Take a look at http://commons.apache.org/collections/which provides for example: public static java.util.Set SetUtils.predicatedSet(set, predicate)
看看http://commons.apache.org/collections/,它提供了例如:public static java.util.Set SetUtils.predicatedSet(set, predicate)
回答by matt b
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?
此处需要更多详细信息 - 您是否尝试按 中包含的对象中的单个字段进行搜索Set?或者只是在Set?
The idea of a Setitself, as the bare interface, has no idea of ordering - you would need to iterate over every element.
Set作为裸接口的 a本身的想法没有排序的想法 - 您需要迭代每个元素。
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.
但是,如果您将自己限制SortedSet在有排序的 ,则可能会利用排序,但由于Sets 不允许随机访问,因此您仍然必须迭代每个元素或了解有关的更多信息该系列不仅是一个Set.
Can you elaborate more on your algorithm and what you are trying to accomplish?
您能否详细说明您的算法以及您要实现的目标?
It is likely that a Setis not the ideal way to represent the data you want to "search" through.
很可能 aSet不是表示要“搜索”的数据的理想方式。
回答by J?rn Horstmann
TreeSet has some methods that might be useful, for example ceilingto search for the next element greater or equals to the search key, floorto get the next lower element. Also headSet, tailSet and subSet to search for parts of a set lower, greater or between given limits.
TreeSet 有一些可能有用的方法,例如ceiling搜索下一个大于或等于搜索关键字的floor元素,以获取下一个较低的元素。还有 headSet、tailSet 和 subSet 来搜索集合中低于、高于或介于给定限制之间的部分。
回答by Tom Tresansky
Try contains(Object o), from the Collectioninterface. The Set interface extends Collection, so all sets are required to implement the Collection methods.
尝试contains(Object o),从Collection界面。Set 接口扩展了 Collection,因此所有集合都需要实现 Collection 方法。
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.
请记住,如果您只知道要搜索的对象是一个集合,则不能保证有任何方法可以在不迭代每个元素的情况下进行搜索,因为此contains()方法可能会也可能不会这样做,具体取决于什么您实际使用的 set 实现类型。
References
参考

