【问题标题】:What Iterable will be used when working with Guava Iterables使用 Guava Iterables 时将使用什么 Iterable
【发布时间】:2016-06-16 12:04:38
【问题描述】:

给定一个如下结构的类:

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 类将使用传递给它的对象中唯一可用的可迭代方法?所以在这种情况下,我们在 this 对象中包含了 List&lt;WebElement&gt; 变量。
  2. 如果 1. 的答案是“是”,如果 Section 类有多个可迭代变量会怎样?其中哪一个将用于迭代?

【问题讨论】:

    标签: java iterator guava iterable


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2013-07-31
      • 1970-01-01
      • 2015-12-13
      • 2011-08-22
      • 2014-04-29
      • 2017-10-01
      • 2018-04-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多