🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Introduction
In Kotlin, the Iterable interface represents a collection of elements that can be iterated over. It is part of the kotlin.collections package and provides the foundation for collection iteration. Any class that implements Iterable can be used in for-loops and other iteration contexts.
Table of Contents
- What is
Iterable? - Creating an
Iterable - Common Operations
- Examples of
Iterable - Real-World Use Case
- Conclusion
1. What is Iterable?
The Iterable interface in Kotlin is a generic interface that defines a collection of elements that can be iterated over. It is part of the kotlin.collections package and is typically implemented by classes that provide sequential access to elements.
Syntax
interface Iterable<out T>
2. Creating an Iterable
You can create an Iterable by implementing the iterator() function, which returns an Iterator.
Example
class MyIterable(private val items: List<String>) : Iterable<String> {
override fun iterator(): Iterator<String> {
return items.iterator()
}
}
3. Common Operations
The Iterable interface provides the following operation:
iterator(): Returns an iterator over the elements in the collection.
4. Examples of Iterable
Example 1: Basic Usage of Iterable
This example demonstrates how to create and use a custom Iterable class.
class MyIterable(private val items: List<String>) : Iterable<String> {
override fun iterator(): Iterator<String> {
return items.iterator()
}
}
fun main() {
val myIterable = MyIterable(listOf("a", "b", "c", "d"))
for (item in myIterable) {
println(item)
}
}
Output:
a
b
c
d
Explanation:
This example creates a custom Iterable class and iterates over its elements using a for-loop.
Example 2: Using Iterable with a List
This example demonstrates how to use the Iterable interface with a list.
fun main() {
val list: Iterable<Int> = listOf(1, 2, 3, 4, 5)
for (element in list) {
println(element)
}
}
Output:
1
2
3
4
5
Explanation:
This example shows how a list implements the Iterable interface, allowing iteration over its elements using a for-loop.
Example 3: Using Iterable with a Set
This example demonstrates how to use the Iterable interface with a set.
fun main() {
val set: Iterable<String> = setOf("Kotlin", "Java", "Python")
for (language in set) {
println(language)
}
}
Output:
Kotlin
Java
Python
Explanation:
This example shows how a set implements the Iterable interface, allowing iteration over its elements using a for-loop.
Example 4: Using Iterable with a Map
This example demonstrates how to use the Iterable interface with a map.
fun main() {
val map: Iterable<Map.Entry<String, Int>> = mapOf("one" to 1, "two" to 2, "three" to 3).entries
for (entry in map) {
println("${entry.key} = ${entry.value}")
}
}
Output:
one = 1
two = 2
three = 3
Explanation:
This example shows how to iterate over the entries of a map, which implements the Iterable interface.
5. Real-World Use Case: Custom Iterable Collection
You can create a custom iterable collection by implementing the Iterable interface.
Example: Custom Iterable Collection
class FibonacciSequence(private val limit: Int) : Iterable<Int> {
override fun iterator(): Iterator<Int> {
return object : Iterator<Int> {
var a = 0
var b = 1
var count = 0
override fun hasNext(): Boolean {
return count < limit
}
override fun next(): Int {
if (!hasNext()) throw NoSuchElementException()
val result = a
val next = a + b
a = b
b = next
count++
return result
}
}
}
}
fun main() {
val fibonacci = FibonacciSequence(10)
for (number in fibonacci) {
println(number)
}
}
Output:
0
1
1
2
3
5
8
13
21
34
Explanation:
This example creates a custom iterable collection for the Fibonacci sequence, allowing iteration over the first n Fibonacci numbers.
Conclusion
The Iterable interface in Kotlin is a powerful and flexible way to define collections of elements that can be iterated over. It is part of the kotlin.collections package and provides the foundation for collection iteration. Understanding and utilizing the Iterable interface can greatly enhance your ability to work with collections in Kotlin.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment