Iterators in Kotlin

Collections (Sets, Maps and Lists) are something we use daily. Traversing (Iteration) is the most common operation we perform over any collection.
Iterators are used to traverse over a collection object. It provides access to the elements of the collection object sequentially without exposing the underlying structure of the collection.

This post was originally posted at https://agrawalsuneet.github.io/blogs/iterators-in-kotlin/ and reposted on Medium on 12th April 2022.

How to get an iterator object?

An iterator reference object can be obtained using iterator() function which is declared in the Iterable interface for generic type T. The Iterable interface is extended by the Collection interface which is implemented by all immutable Sets, Maps and Lists.

/**
* Classes that inherit from this interface can be represented as a sequence of elements that can
* be iterated over.
* @param T the type of element being iterated over. The iterator is covariant in its element type.
*/
public interface Iterable<out T> {
/**
* Returns an iterator over the elements of this object.
*/
public operator fun iterator(): Iterator<T>
}

The Iterable has one more variant interface as MutableIterable which has the same iterator() function but this interface is extended by MutableCollection which is implemented by mutable Sets, Maps and Lists.

/**
* Classes that inherit from this interface can be represented as a sequence of elements that can
* be iterated over and that supports removing elements during iteration.
* @param T the type of element being iterated over. The mutable iterator is invariant in its element type.
*/
public interface MutableIterable<out T> : Iterable<T> {
/**
* Returns an iterator over the elements of this sequence that supports removing elements during iteration.
*/
override fun iterator(): MutableIterator<T>
}

How to use an iterator object?

Please continue reading at https://agrawalsuneet.github.io/blogs/iterators-in-kotlin/

That’s all for now. You can read my other interesting blogs here or you can enjoy my games or apps listed here. Feel free to use my open-source Android components in your app listed here. Or drop an email, if you didn’t find what you are looking for and need some help.

--

--

--

https://agrawalsuneet.github.io/

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

String Palindrome in java

How to Create Code Confidence: a Guide for Optimizing Testing, Code Standardization and…

How many employees does it take to change a light bulb?

My 100 days of learning challenge

Top Reasons For Outsourcing Software Development To Serbia In 2021

Qlik Extension and Qlik Server Side Extension, what are them

Amazon Elastic Kubernetes(EKS)

Customise your zsh terminal and prompt in MacOS!

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Suneet Agrawal

Suneet Agrawal

https://agrawalsuneet.github.io/

More from Medium

Infix Function Kotlin

Pair and Triple — Kotlin

How and When to convert a Utility function in Kotlin to an Extension Function

LiveData in Kotlin