java 包含 Iterable 和 Iterator 的方法吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26589279/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 10:12:30  来源:igfitidea点击:

Contains method for Iterable and Iterator?

javacollectionsiteratoriterable

提问by Roland

Is there a simple method to check if an element is contained in an iterable or iterator, analogous to the Collection.contains(Object o)method?

是否有一种简单的方法来检查元素是否包含在可迭代或迭代器中,类似于Collection.contains(Object o)方法?

I.e. instead of having to write:

即而不是必须写:

Iterable<String> data = getData();
for (final String name : data) {
    if (name.equals(myName)) {
        return true;
    }
}

I would like to write:

我想写:

Iterable<String> data = getData(); 
if (Collections.contains(data, myName)) {
    return true;
}

I'm really surprised there is no such thing.

我真的很惊讶没有这样的事情。

回答by Stuart Marks

In Java 8, you can turn the Iterableinto a Streamand use anyMatchon it:

在 Java 8 中,您可以将Iterable变成 aStreamanyMatch在其上使用:

String myName = ... ;
Iterable<String> data = getData();

return StreamSupport.stream(data.spliterator(), false)
                    .anyMatch(name -> myName.equals(name));

or using a method reference,

或使用方法引用,

return StreamSupport.stream(data.spliterator(), false)
                    .anyMatch(myName::equals);

回答by MikeFHay

Guava has the functions Iterables.containsand Iterators.containswhich do what you want. You can look at the sourceto see how to implement them yourself.

Guava 有函数Iterables.containsIterators.contains可以做你想做的事。您可以查看源代码以了解如何自己实现它们。

回答by Wolf S

An iterator is a sort of cursor which can be moved over the elements of any collection of elements. So its inner state is mainly the pointer to the current element. If you would try to find out whether it "contains" a certain element you would have to move the cursor and therefore modify the inner state. Modifying the state by simply asking a question is surely a bad thing to do.

迭代器是一种光标,可以在任何元素集合的元素上移动。所以它的内部状态主要是指向当前元素的指针。如果您试图找出它是否“包含”某个元素,您将不得不移动光标并因此修改内部状态。通过简单地提出问题来修改状态肯定是一件坏事。

That is even the problem with the mentioned Guava. It will modify the iterator object by simply calling the contains method.

这甚至是提到的番石榴的问题。它将通过简单地调用 contains 方法来修改迭代器对象。

An iterable on the other hand is simply an interface telling the compiler that there is something to iterate over. In most cases the iterable object will be the collection itself. If you would add methods like "contains" to the interface Iterable you would get a (simplified) version of the Collection interface - which already exists. So there is no need for that.

另一方面,iterable 只是一个接口,告诉编译器有一些东西要迭代。在大多数情况下,可迭代对象将是集合本身。如果您将“包含”之类的方法添加到接口 Iterable,您将获得 Collection 接口的(简化)版本 - 已经存在。所以没有必要。

If you are stuck in your code at some place where you have a reference to an iterable but need functionality of a collection you should think about refactoring your code. Either you should use the interface collection consistently or ask yourself why you should better not call collection methods at this point. So your problem is most probably a result of a suboptimal code design.

如果您在某个地方被困在您的代码中,您可以引用可迭代但需要集合的功能,您应该考虑重构您的代码。要么您应该始终如一地使用接口集合,要么问问自己为什么此时最好不要调用集合方法。因此,您的问题很可能是次优代码设计的结果。

On the other hand I would consider it to be a strange thing to use Iterable as type for parameters or variables anyway. Technically you can do this but I think it is meant to be used in loops only.

另一方面,我认为无论如何使用 Iterable 作为参数或变量的类型是一件奇怪的事情。从技术上讲,您可以这样做,但我认为它只能在循环中使用。

回答by TheLostMind

An Iteratoris like a pointer/reference to an element in a collection. It is notthe collection itself. So, you can't use iterator.contains(). Also, an Iterableinterface is used to iterateover a collection using a for-eachloop. A collection and Iterator / Iterable are different.

AnIterator就像一个指向集合中元素的指针/引用。它不是集合本身。所以,你不能使用iterator.contains(). 此外,Iterable接口用于使用for-each循环迭代集合。集合和 Iterator / Iterable 是不同的。

回答by Guy Marom

From the JavaDoc for Iterable:

JavaDoc for Iterable

Implementing this interface allows an object to be the target of the "foreach" statement

实现这个接口允许一个对象成为“foreach”语句的目标

I'm sorry to say what you're trying to do is impossible.

我很抱歉地说你想要做的事情是不可能的。

.containsis a method of the interface Collection and not Iterable so maybe you can use that interface.

.contains是接口 Collection 的一种方法,而不是 Iterable 所以也许您可以使用该接口。