Java Iterable 接口有什么用?

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

What is the Iterable interface used for?

javaiterable

提问by Johanna

I am a beginner and I cannot understand the real effect of the Iterableinterface.

我是初学者,无法理解Iterable界面的真实效果。

采纳答案by Michael Myers

Besides what Jeremy said, its main benefit is that it has its own bit of syntactic sugar: the enhanced for-loop. If you have, say, an Iterable<String>, you can do:

除了 Jeremy 所说的,它的主要好处是它有自己的语法糖:增强的 for-loop。如果你有,说,一个Iterable<String>,你可以这样做:

for (String str : myIterable) {
    ...
}

Nice and easy, isn't it? All the dirty work of creating the Iterator<String>, checking if it hasNext(), and calling str = getNext()is handled behind the scenes by the compiler.

很好很容易,不是吗?编译器在幕后处理所有创建Iterator<String>、检查是否hasNext()和调用的肮脏工作str = getNext()

And since most collections either implement Iterableor have a view that returns one (such as Map's keySet()or values()), this makes working with collections much easier.

而且由于大多数集合要么实现Iterable要么具有返回一个的视图(例如Map'skeySet()values()),这使得处理集合变得更加容易。

The IterableJavadocgives a full list of classes that implement Iterable.

Iterable的Javadoc给出了实现类的完整列表Iterable

回答by Jeremy Smyth

If you have a complicated data set, like a tree or a helical queue (yes, I just made that up), but you don't carehow it's structured internally, you just want to get all elements one by one, you get it to return an iterator.

如果你有一个复杂的数据集,比如一棵树或一个螺旋队列(是的,我只是编造的),但你并不关心它的内部结构,你只想一个一个地获取所有元素,你明白了返回一个迭代器。

The complex object in question, be it a tree or a queue or a WombleBasket implements Iterable, and can return an iterator object that you can query using the Iterator methods.

有问题的复杂对象,无论是树、队列还是 WombleBasket 都实现了 Iterable,并且可以返回一个迭代器对象,您可以使用 Iterator 方法查询该对象。

That way, you can just ask it if it hasNext(), and if it does, you get the next()item, without worrying where to get it from the tree or wherever.

这样,您就可以询问它是否存在hasNext(),如果是,您就可以得到该next()项目,而无需担心从树上的何处或任何地方获取它。

回答by Zack Marrapese

Iterators basically allow for iteration over any Collection.

迭代器基本上允许对任何集合进行迭代。

It's also what is required to use Java's for-each control statement.

这也是使用 Java 的 for-each 控制语句所需要的。

回答by Peter Kofler

It returns an java.util.Iterator. It is mainly used to be able to use the implementing type in the enhanced for loop

它返回一个java.util.Iterator. 主要用于在增强的for循环中能够使用实现类型

List<Item> list = ...
for (Item i:list) {
 // use i
}

Under the hood the compiler calls the list.iterator()and iterates it giving you the iinside the for loop.

在引擎盖下,编译器调用list.iterator()并迭代它给你ifor 循环的内部。

回答by Matt Bridges

An interface is at its heart a list of methods that a class should implement. The iterable interface is very simple -- there is only one method to implement: Iterator(). When a class implements the Iterableinterface, it is telling other classes that you can get an Iteratorobject to use to iterate over (i.e., traverse) the data in the object.

接口的核心是一个类应该实现的方法列表。可迭代接口非常简单——只有一种方法可以实现:Iterator(). 当一个类实现该Iterable接口时,它告诉其他类您可以获得一个Iterator对象来用于迭代(即,遍历)对象中的数据。