iterator vs for循环和为什么迭代器被引入,因为我们有循环?

问题描述:


可能重复:


for循环与a之间存在性能差异for-each循环?

下面的代码显示同时包含for循环与迭代器一样,我们可以迭代集合的元素然后for循环和迭代器之间的区别是什么以及为什么我们应该在集合的情况下只使用迭代器

Below code shows that with both for loop as well as with iterator we can iterate the elements of collection then what is the difference between for loop and iterator and why we should use only iterator in case of collection

    ArrayList<String> list=new ArrayList<String>();
    list.add("dipu");
    list.add("alok");
    list.add("alok");
    list.add("jyoti");
    ArrayList<Integer> al=new ArrayList<Integer>();
    al.add(1);
    al.add(2);
    String a[]={"a","b"};
    for(int i=0;i<list.size();i++)
    {
        System.out.println(list.get(i));;
    }
    for(Integer t:al)
    {
        System.out.println(t);
    }
    for (Iterator iter = list.iterator(); iter.hasNext();)
        {
        System.out.println(iter.next());
        }
    Iterator it=list.iterator();
    while(it.hasNext())
    {
    String st=it.next().toString();
    System.out.println(st);
    }


虽然我不熟悉使用Java Iterator,它看起来与.NET的IEnumerable非常相似。

Though I'm not familiar with the Java Iterator, it seems very similar to .NET's IEnumerable.

枚举器/迭代器的优点是:

The advantages of the enumerator/iterator are:


  • 您不必知道集合的大小,在某些情况下可能需要N个步骤来确定,增加执行时间(尽管它在技术上保持线性)。相反,你只是继续移动到下一个元素,直到没有。

  • You don't have to know the size of the collection, which in some cases can require N steps to determine, increasing execution time (though it remains technically linear). Instead, you just keep moving to the next element until there aren't any.

因为集合的基数不必知道,迭代器可以允许动态生成集合,或者在开始处理已有内容时添加元素进行流式处理。例如,您可以从Iterator和/或重载迭代器getter派生,以创建生成有限或无限系列懒惰的类,在您要求它时,而不是在定义集合时,确定可枚举集合中的每个元素。您还可以设置一个缓冲流,您可以在其中处理已收到的记录,数据包等,而另一个线程或进程可以在您之前排队等待更多工作。

Because the cardinality of the collection doesn't have to be known, iterators can allow collections to be generated dynamically, or "streamed" with elements being added while you begin work on what you already have. For instance, you could derive from Iterator and/or overload iterator getters to create classes that generate finite or infinite series "lazily", figuring out what each element in your enumerable collection is when you ask for it instead of when you define the collection. You could also set up a buffered stream, where you process records, packets, etc that you have received, while another thread or process works ahead of you to queue up more for you to work on.

任何可以提供迭代器的集合都可以以完全相同的方式遍历,而不必知道它是否可索引,定义大小的方法或成员等等等。迭代器实现因此提供了一个适配器,允许相同的代码处理传递给它的任何集合。

Any collection that can provide an iterator can be traversed in exactly the same way, instead of having to know whether it's indexable, what the method or member is that defines size, etc etc etc. Iterator implementations thus provide an adapter to allow the same code to work on any collection passed to it.

Java是否具有与.NET扩展方法等效的方法(静态方法,不是类定义的一部分,但是在类型的实例上工作并且可以被调用,就好像它们是实例方法一样)?如果是这样,您可以定义采用迭代器并生成结果的方法,这可能是另一个迭代器。 .NET的Linq库在很大程度上基于这些,提供了一个非常强大的集合操作框架,允许将常见操作链接在一起,每个操作都依赖于前一操作的结果。

Does Java have an equivalent to .NET extension methods (static methods that are not part of the class definition, but that work on instances of the type and can be called as if they were instance methods)? If so, you can define methods that take an Iterator and produce a result, which could be another Iterator. .NET's Linq library is based heavily on these, providing a very powerful collection-manipulation framework allowing for common operations to be chained together, each operating on the result of the previous operation.