java 外部迭代器与内部迭代器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/224648/
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
External Iterator vs Internal Iterator
提问by
What is an external and internal iterator in Java ?
Java 中的外部和内部迭代器是什么?
回答by Johann Zacharee
External Iterator
外部迭代器
When you get an iterator and step over it, that is an external iterator
当你得到一个迭代器并越过它时,它就是一个外部迭代器
for (Iterator iter = var.iterator(); iter.hasNext(); ) {
Object obj = iter.next();
// Operate on obj
}
Internal Iterator
内部迭代器
When you pass a function object to a method to run over a list, that is an internal iterator
当你将一个函数对象传递给一个方法来运行一个列表时,它就是一个内部迭代器
var.each( new Functor() {
public void operate(Object arg) {
arg *= 2;
}
});
回答by Chris Kimpton
I found this description:
我找到了这个描述:
External vs. internal iterators.
外部与内部迭代器。
External Iterators - when the iteration is controlled by the collection object we say that we have an external Iterator.
外部迭代器 - 当迭代由集合对象控制时,我们说我们有一个外部迭代器。
In languages like .net or java it's very easy to create external iterators. In our classical implementation an external iterator is implemented. In the following example an external iterator is used:
在 .net 或 java 等语言中,创建外部迭代器非常容易。在我们的经典实现中,实现了一个外部迭代器。在以下示例中,使用了外部迭代器:
// using iterators for a clloection of String objects:
// using in a for loop
for (Iterator it = options.iterator(); it.hasNext(); ) {
String name = (String)it.next();
System.out.println(name);
}
// using in while loop
Iterator name = options.iterator();
while (name.hasNext() ){
System.out.println(name.next() );
}
// using in a for-each loop (syntax available from java 1.5 and above)
for (Object item : options)
System.out.println(((String)item));
Internal Iterators - When the iterator controls it we have an internal iterator
内部迭代器 - 当迭代器控制它时,我们有一个内部迭代器
On the other side implementing and using internal iterators is really difficult. When an internal iterator is used it means that the code is be run is delegated to the aggregate object. For example in languages that offer support for this is easy to call internal iterators:
另一方面,实现和使用内部迭代器真的很困难。当使用内部迭代器时,意味着运行的代码被委托给聚合对象。例如,在提供支持的语言中,很容易调用内部迭代器:
collection do: [:each | each doSomething] (Smalltalk)
The main idea is to pass the code to be executed to the collection. Then the collection will call internally the doSomething method on each of the components. In C++ it's possible to send the doMethod method as a pointer. In C#, .NET or VB.NET it is possible to send the method as a delegate. In java the Functordesign pattern has to be used. The main idea is to create a base Interface with only one method (doSomething). Then the method will be implemented in a class which implements the interface and the class will be passed to the collection to iterate. For more details see the Functordesign pattern.
主要思想是将要执行的代码传递给集合。然后集合将在内部调用每个组件上的 doSomething 方法。在 C++ 中,可以将 doMethod 方法作为指针发送。在 C#、.NET 或 VB.NET 中,可以将方法作为委托发送。在javaFunctor中必须使用设计模式。主要思想是创建一个只有一个方法(doSomething)的基础接口。然后该方法将在一个实现接口的类中实现,并且该类将被传递给集合进行迭代。有关更多详细信息,请参阅Functor设计模式。
回答by Chris Kimpton
Example of external iterator:
外部迭代器示例:
int count = 0;
Iterator<SomeStaff> iterator = allTheStaffs.iterator();
while(iterator.hasNext()) {
SomeStaff staff = iterator.next();
if(staff.getSalary() > 25) {
count++;
}
}
Example of internal iterator:
内部迭代器示例:
long count = allTheStaffs.stream()
.filter(staff -> staff.getSalary() > 25)
.count();
In images:
在图像中:
回答by VonC
It is about who controls the iteration.
这是关于谁控制迭代。
Other details are in this question What are the benefits of the Iterator interface in Java?
其他细节在这个问题中 Java 中的 Iterator 接口有什么好处?
回答by Kishon
I found the answer over here.
我在这里找到了答案。
Internal Iterators manage the iterations in the background. This leaves the programmer to just declaratively code what is meant to be done with the elements of the Collection, rather than managing the iteration and making sure that all the elements are processed one-by-one. Ex:
内部迭代器在后台管理迭代。这让程序员只需要声明性地对集合的元素进行编码,而不是管理迭代并确保逐一处理所有元素。前任:
public class InternalIterator {
public static void main(String args[]){
List<String> namesList=Arrays.asList("Tom", "Dick", "Harry");
namesList.forEach(name -> System.out.println(name));//Internal Iteration
}
}
With external iterators responsibility of iterating over the elements, and making sure that this iteration takes into account the total number of records, whether more records exist to be iterated and so on lies with the programmer.
外部迭代器负责对元素进行迭代,并确保此迭代考虑到记录总数、是否存在更多要迭代的记录等取决于程序员。
Ex:
前任:
import java.util.*;
public class ExternalIterator {
public static void main(String args[]){
List<String> namesList=Arrays.asList("Tom", "Dick", "Harry");
for(String name:namesList){
System.out.println(name);
}
}
}
回答by Manas
External Iterator:- Using this we have to iterate all element one-by-one and do some operation because programmer does have control on that, its a External Iterator.
外部迭代器:- 使用它,我们必须逐一迭代所有元素并进行一些操作,因为程序员确实可以控制它,它是一个外部迭代器。
Internal Iterator:- Using this we can iterate according to our condition, Programmer can control over on it, its a Internal Iterator .
内部迭代器:- 使用它我们可以根据我们的条件进行迭代,程序员可以控制它,它是一个内部迭代器。
Lets see one example below : Q - we want to add sum on integer from a list which is equal or more that 5.
让我们看下面的一个例子: Q - 我们想从一个列表中添加一个等于或大于 5 的整数总和。
package java8;
import java.util.ArrayList;
import java.util.List;
public class IteratorExpr {
public static void main(String[] args) {
List<Integer> myList = new ArrayList<Integer>();
for(int i=0; i<10; i++) myList.add(i);
//Get sum of all value which is more than 5 using External Iterator
int sum = 0;
for(int no: myList) {
if(no >=5) {
sum += no;
}
}
System.out.println("Sum of numbers using External api : "+sum);
int summ = myList.stream()
.filter(p->p>=5)
.mapToInt(p->p).sum();
System.out.println("Sum of numbers using internal api : "+summ);
}
}
Output :
输出 :
Sum of numbers using External api : 35
Sum of numbers using internal api : 35


