What is an external and internal iterator in Java ?
问题:
回答1:
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;
}
});
回答2:
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:
// 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 Functor
design 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 Functor
design pattern.
回答3:
It is about who controls the iteration.
Other details are in this question What are the benefits of the Iterator interface in Java?
回答4:
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);
}
}
}
回答5:
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:
回答6:
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.
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