Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

External Iterator vs Internal Iterator

Tags:

java

iterator

People also ask

What is internal iterator?

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.

Which is example of internal iteration?

Internal Iteration The Collection. forEach() and Stream. forEach() are examples of external iterations.

Is Java design is based on the internal iteration?

There can be no doubt the Java Stream API was designed with the Iterator pattern in mind, and particularly the internal version of the pattern, because Stuart Marks and Brian Goetz have both graciously posted about their design decisions, here on SO.

What is difference between iterator and for each loop?

Difference between the two traversalsIn for-each loop, we can't modify collection, it will throw a ConcurrentModificationException on the other hand with iterator we can modify collection. Modifying a collection simply means removing an element or changing content of an item stored in the collection.


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;
  }
});

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.


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:

enter image description here


It is about who controls the iteration

Other details are in this question What are the benefits of the Iterator interface in Java?


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);
      }

   }

}