
- Kotlin - Home
- Kotlin - Overview
- Kotlin - Environment Setup
- Kotlin - Architecture
- Kotlin - Basic Syntax
- Kotlin - Comments
- Kotlin - Keywords
- Kotlin - Variables
- Kotlin - Data Types
- Kotlin - Operators
- Kotlin - Booleans
- Kotlin - Strings
- Kotlin - Arrays
- Kotlin - Ranges
- Kotlin - Functions
- Kotlin Control Flow
- Kotlin - Control Flow
- Kotlin - if...Else Expression
- Kotlin - When Expression
- Kotlin - For Loop
- Kotlin - While Loop
- Kotlin - Break and Continue
- Kotlin Collections
- Kotlin - Collections
- Kotlin - Lists
- Kotlin - Sets
- Kotlin - Maps
- Kotlin Objects and Classes
- Kotlin - Class and Objects
- Kotlin - Constructors
- Kotlin - Inheritance
- Kotlin - Abstract Classes
- Kotlin - Interface
- Kotlin - Visibility Control
- Kotlin - Extension
- Kotlin - Data Classes
- Kotlin - Sealed Class
- Kotlin - Generics
- Kotlin - Delegation
- Kotlin - Destructuring Declarations
- Kotlin - Exception Handling
Kotlin List - iterator() Function
The Kotlin List iterator() function is used to return an iterator for traversing elements in a list. Iterators are useful when we need to process each element of the list one by one.
There are the following use cases of the iterator() function:
- Custom Traversal Logic: It provides fine-grained control over traversal, including conditional checks or early exit.
- Compatibility: This function works with all Kotlin collection.
- Mutable Iterators: For mutable collections, the MutableIterator also includes the remove() function to remove elements during iteration safely.
Syntax
Following is the syntax of Kotlin list iterator() function −
val iterator = List.iterator()
Parameters
This function does not accepts any parameters
Return value
This function returns an iterator object of type Iterator<T>. For mutable collection, it returns a MutableIterator<T>.
Example 1
Let's see a basic example of the iterator() function, which iterates over elements of list.
fun main(args: Array<String>) { val list = listOf(10, 20, 30, 40, 50) // Get the iterator val iterator = list.iterator() // Traverse using iterator functions while (iterator.hasNext()) { val element = iterator.next() println("Element: $element") } }
Output
Following is the output −
Element: 10 Element: 20 Element: 30 Element: 40 Element: 50
Example 2: Removing Element During Iteration
The following example demonstrate the flexibility of iterator() function, when working with mutable collection −
fun main(args: Array<String>) { val numbers = mutableListOf(1, 2, 3, 4, 5) val iterator = numbers.iterator() while (iterator.hasNext()) { val number = iterator.next() if (number % 2 == 0) { // Remove even numbers iterator.remove() } } println(numbers) }
Output
Following is the output −
[1, 3, 5]
Example 3: Removing String
This is another example of iterator() function to remove strings having less than 5 character in the mutableList −
fun main(args: Array<String>) { val words = mutableListOf("apple", "bat", "cat", "elephant", "dog", "frog") // Get the iterator val iterator = words.iterator() // Remove words less than 5 characters while (iterator.hasNext()) { val word = iterator.next() if (word.length < 5) { iterator.remove() } } println("Filtered List: $words") }
Output
Following is the output −
Filtered List: [apple, elephant]