[Java] Internal Iterator Note

Introduction

When I was studying Stream and forEach, I came across an unknown word called internal iterator, so a memo at that time. It's probably appropriate because I just understood it myself.

Does the iterator have an inside and an outside?

When I learned the word internal iterator, I was surprised that there are two types of iterators: internal iterators and external iterators. Then I wondered what iterator I knew, so I first investigated iterator.

An iterator (English: iterator) is an iterator abstraction for each element of an array or similar collective data structure (collection or container) in a programming language. In a real programming language, it appears as an object or grammar. In JIS, it is translated as an iterator (Hampukushi) [1] [2].

Quote: Iterator-Wikipedia

Is it an abstraction of iterative processing? I feel like I somehow understood this. There are two types of this abstraction, external and internal. Now let's look at the difference between the two.

External iterator

In Java, the object that implements the java.util.Iterator interface family is the external iterator. Java 1.5 and later Iterator supports generics.

Quote: Iterator-Wikipedia

In other words, what I recognized as an iterator was an external iterator. So, the implementation of the external iterator looks like this.

External iterator


Iterator<Hoge> iterator =The one that implements Iterable.iterator()
while (iterator.hasNext()) {
    // iterator.next()Something to do
}

I wrote it quite appropriately, but by implementing an interface called Iterable and having an Iterator, it is possible to abstract the iterative process. Is it called an external iterator because the while repeating is exposed to the outside?

Internal iterator

The type of repetition method in which a method that expresses the processing for each element, such as a Ruby block, is passed to the method of the container object and the method recalls the processing for each element is called an "internal iterator".

Internal iterators do not create extra classes and are easy to use and create. However, if the language does not support closures, it will be necessary to devise ways to share information between the loop body and the outside, and as seen in the C language example, the usability as a loop will deteriorate. For this reason, external iterators are used in C ++ and Java, which do not have closures.

Quote: Matsumoto Naoden Programming Okite 5th (2)

I'm not sure, so I'll look at the implementation.

Internal iterator


List<Integer> list =Something list;
list.forEach(n -> n + 1);

//Imaginary for Each
forEach(processing) {
    for (repetition) {
        //Call process;
    }
}

In other words, the repetition is inside and passes the processing you want to do. That's why I call it an internal iterator. In Java, there is no first-class function, so pass a functional interface.

So, what I'm not sure about here is that it's difficult to implement an internal iterator if it doesn't support closures. Why do you need closures?

closure

I didn't know much about closures in the first place, so I looked it up. I understand that a function holds variables other than inside the function as a scope. I've researched a lot here as well, so I'd like to write another article.

closure


function createCounter() {
  let count = 0;

  return function() {
    count++;
    console.log(count);
  };
}

let counter  = createCounter();
counter(); // 1
counter(); // 2

With JS, it looks like this.

Why internal iterators need closures

At first I had no idea why I needed it. I somehow understood it while researching closures. Java sample code for the time being.

Internal iterator and closure


final List<Integer> list =Something list
final Integer n =Some number;
list.forEach(value -> value + n);

//Imaginary for Each
forEach(processing) {
    for (repetition) {
        //Call process;
    }
}

At this time, due to the closure mechanism, n can be used by entering the scope even in the process passed to forEach. If there is no closure, it is necessary to prepare a mechanism to pass arguments or have it as a field to be repeated. In other words, closures make implementation easier.

Summary

Iterators include external iterators and internal iterators. The internal iterator looks smarter personally. However, without closures, implementing an internal iterator would be cumbersome.

Digression

When I usually study, I take notes, so I wrote an article based on them. I can't write anything more appropriate than people can see, and is the source of the citation really correct? I need to think about it. Well, this time I wrote it as a trial, so I think there is something appropriate, but I want to take more time when I write something next time.

Also, I wrote mainly about Java, but closures in Java seem to be a little different from other functional languages. I saw an article saying, "Java 8 lambdas aren't closures, but you can do what you want with closures, and you can't do what you can't do, so there's nothing wrong with it." I'm not sure about this, so I'll investigate more.

Recommended Posts

[Java] Internal Iterator Note
Java abstract modifier [Note]
Java JUnit brief note
[java] Java SE 8 Silver Note
[Note] Java: String search
[Note] Java: String survey
My Study Note (Java)
java: Add date [Note]
A note about Java GC
(Note) Java classes / variables / methods
Java and Iterator Part 1 External Iterator
[Java] [Spring] Spring Boot 1.4-> 1.2 Downgrade Note
Study Java with Progate Note 1
Note
[Note] Java Silver SE8 qualification acquired
[Java] Points to note with Arrays.asList ()
Note
Note 1: Elementary, stumbling blocks (java, javascript)
Java
[Note] Handling of Java decimal point
My note: Introducing Java to Ubuntu
How to turn Iterator Dojo (Java)
Java
Note: Differences from Java as seen from C #
Java Agent Actual Anti-Serialized Injection Internal Shell
[Java] Get List / Map elements with Iterator
Pass parameters when debugging vscode java. [Note]
[Beginner] Java method / class / external library [Note 23]
Java SE 8 Silver (Java SE 8 Programmer I) Pass Note
Basic knowledge of Java development Note writing
Note: next ・ nextLine (paiza learning Java introduction 9: # 06)
[Note] Cooperation between Java and DB (basic)
How to use Java Scanner class (Note)