External vs. Internal Iterators
We'll cover the following
Programmers with experience in Java and other C-like languages are used to external iterators. They are common but complex; we’re very familiar with them, but they involve many moving parts. Internal iterators are less familiar to a lot of programmers, but they are less complex than external iterators.
External iterators
External iterators in Kotlin are already an improvement when compared to external iterators in Java, but even then, the internal iterators have much less ceremony, are more concise, and are expressive when compared to Kotlin’s external iterators. Let’s compare Kotlin’s external iterators with internal iterators using some examples.
Here’s a list of numbers stored in a numbers
variable:
// iterate.kts
val numbers = listOf(10, 12, 15, 17, 18, 19)
To print only the even numbers in this list we can use the for...in
loop of Kotlin,
like so:
Get hands-on with 1200+ tech skills courses.