java - 使用 Guava Iterables 时将使用什么 Iterable

标签 java iterator guava iterable

给定以下结构的类:

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 类型的对象进行操作的静态实用方法。 .

现在在我上面描述的类中,this作为可迭代对象传递给 private int indexOf()方法。

问题:

  1. 我要在this中迭代什么目的?我的假设是对的吗 Iterables类将使用传递给它的对象中唯一可用的可迭代方法?所以在这种情况下我们有 List<WebElement>里面的变量 this对象。
  2. 如果 1. 的答案是"is",那么 Section 会发生什么?类有多个可迭代变量吗?其中哪一个将用于迭代?

最佳答案

Iterables.indexOf() 将实现 Iterable 接口(interface)的对象作为其第一个参数。因此,Iterables.indexOf() 迭代的内容是由作为参数传入的对象定义的,在您的示例中是 Section 类。但它不使用变量 - 它将调用 Section 对象上的 Iterable.iterator() 方法。不可能有多个这些方法,因此不会出现对 Iterables.indexOf() 将迭代的内容产生混淆的情况。

关于java - 使用 Guava Iterables 时将使用什么 Iterable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35767504/

相关文章:

java - 表更新时触发 Guava 缓存刷新

java - Guava 不可变集合的序列化是否稳定?

java - 如何从 java 类中读取内容?

java - 实现双向连接的组合框

java - 我应该把 bluecove-2.1.0 文件夹放在哪里

c++ - 遍历 map C++

java - 为什么在枚举类中阻止默认序列化

java - 使用 HashMap 对相似的项目进行分组

python - 从每行数据框中的列中删除不在列表中的字符串

java - 无法从 Tomcat 中运行的 Guava r08 加载类。无法从源代码编译。缺少 javax.annotations