使用 Guava Iterables 时将使用什么 Iterable
What Iterable will be used when working with Guava Iterables
给定以下结构的 class:
public class Section extends IterableWidgetTemplate<Item>{
private List<WebElement> items1;
// other non iterable methods
private int indexOf(final Item item) {
int i = Iterables.indexOf(this, new Predicate<Item>() {
. . .
});
return i;
}
其中 Iterables
是一个 Guava com.google.common.collect.Iterables
,根据其文档,它包含对 Iterable
.
类型的对象进行操作的静态实用方法
现在,在我上面描述的 class 中,this
作为可迭代对象传递给 private int indexOf()
方法。
问题:
- 我要在
this
对象中迭代什么?我是否正确地假设 Iterables
class 将使用传递给它的对象中唯一可用的可迭代方法?所以在这种情况下,我们在 this
对象中有 List<WebElement>
变量。
- 如果 1. 的答案是 "yes",如果
Section
class 有多个可迭代变量会怎样?其中哪一个将用于迭代?
Iterables.indexOf()
的第一个参数是一个实现了 Iterable
接口的对象。因此,Iterables.indexOf()
迭代的内容由作为参数传入的对象定义,在您的示例中为 Section
class。但是,它没有使用变量 - 它会调用 Section
对象上的 Iterable.iterator()
方法。不可能有不止一种方法,因此不会出现对 Iterables.indexOf()
将迭代的内容产生混淆的情况。
给定以下结构的 class:
public class Section extends IterableWidgetTemplate<Item>{
private List<WebElement> items1;
// other non iterable methods
private int indexOf(final Item item) {
int i = Iterables.indexOf(this, new Predicate<Item>() {
. . .
});
return i;
}
其中 Iterables
是一个 Guava com.google.common.collect.Iterables
,根据其文档,它包含对 Iterable
.
现在,在我上面描述的 class 中,this
作为可迭代对象传递给 private int indexOf()
方法。
问题:
- 我要在
this
对象中迭代什么?我是否正确地假设Iterables
class 将使用传递给它的对象中唯一可用的可迭代方法?所以在这种情况下,我们在this
对象中有List<WebElement>
变量。 - 如果 1. 的答案是 "yes",如果
Section
class 有多个可迭代变量会怎样?其中哪一个将用于迭代?
Iterables.indexOf()
的第一个参数是一个实现了 Iterable
接口的对象。因此,Iterables.indexOf()
迭代的内容由作为参数传入的对象定义,在您的示例中为 Section
class。但是,它没有使用变量 - 它会调用 Section
对象上的 Iterable.iterator()
方法。不可能有不止一种方法,因此不会出现对 Iterables.indexOf()
将迭代的内容产生混淆的情况。