The elementAtOrElse function in Kotlin is used to retrieve an element at a specified index in a sequence or return a default value if the index is out of bounds. It is part of the Kotlin standard library and provides a way to access elements based on their position within a sequence while handling out-of-bounds scenarios gracefully.
Table of Contents
- Introduction
elementAtOrElseFunction Syntax- Understanding
elementAtOrElse - Examples
- Basic Usage
- Retrieving an Element in a Sequence of Strings
- Using
elementAtOrElsewith Custom Objects - Chaining
elementAtOrElsewith Other Functions
- Real-World Use Case
- Conclusion
Introduction
The elementAtOrElse function allows you to access an element at a specified index in a sequence or return a default value if the index is out of bounds. This is useful for scenarios where you need to safely retrieve elements based on their position, avoiding IndexOutOfBoundsException.
elementAtOrElse Function Syntax
The syntax for the elementAtOrElse function is as follows:
fun <T> Sequence<T>.elementAtOrElse(index: Int, defaultValue: (Int) -> T): T Parameters:
index: The index of the element to retrieve from the sequence.defaultValue: A lambda function that returns a default value if the index is out of the range of the sequence.
Returns:
- The element at the specified index or the default value if the index is out of bounds.
Understanding elementAtOrElse
The elementAtOrElse function works by iterating through the sequence until it reaches the specified index. If the index is within bounds, it returns the element at that index. If the index is out of bounds, it calls the defaultValue function and returns the result.
Examples
Basic Usage
To demonstrate the basic usage of elementAtOrElse, we will create a sequence of integers and retrieve an element at a specific index with a default value if the index is out of bounds.
Example
fun main() { val numbers = sequenceOf(1, 2, 3, 4, 5) val element = numbers.elementAtOrElse(10) { -1 } println(element) // Output: -1 } Output:
-1 Retrieving an Element in a Sequence of Strings
This example shows how to retrieve an element at a specified index in a sequence of strings with a default value if the index is out of bounds.
Example
fun main() { val names = sequenceOf("Arjun", "Bhaskar", "Chitra", "Deepak", "Esha") val element = names.elementAtOrElse(6) { "Unknown" } println(element) // Output: Unknown } Output:
Unknown Using elementAtOrElse with Custom Objects
You can use the elementAtOrElse function to retrieve an element at a specific index in a sequence of custom objects with a default value if the index is out of bounds.
Example
data class Person(val name: String, val age: Int) fun main() { val people = sequenceOf( Person("Arjun", 25), Person("Bhaskar", 30), Person("Chitra", 22), Person("Deepak", 28), Person("Esha", 26) ) val element = people.elementAtOrElse(10) { Person("Unknown", 0) } println(element) // Output: Person(name=Unknown, age=0) } Output:
Person(name=Unknown, age=0) Chaining elementAtOrElse with Other Functions
The elementAtOrElse function can be chained with other sequence functions to perform more complex operations before retrieving the element at a specific index with a default value if the index is out of bounds.
Example
fun main() { val numbers = sequenceOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) val element = numbers.filter { it % 2 == 0 } .elementAtOrElse(10) { -1 } println(element) // Output: -1 } Output:
-1 Real-World Use Case
Retrieving a Specific Product
In real-world applications, the elementAtOrElse function can be used to retrieve a specific product from a sequence of products based on its position, with a default value if the index is out of bounds.
Example
data class Product(val name: String, val price: Double) fun main() { val products = sequenceOf( Product("Laptop", 1000.0), Product("Smartphone", 600.0), Product("Tablet", 300.0), Product("Headphones", 150.0) ) val element = products.elementAtOrElse(5) { Product("Unknown", 0.0) } println(element) // Output: Product(name=Unknown, price=0.0) } Output:
Product(name=Unknown, price=0.0) Conclusion
The elementAtOrElse function in Kotlin provides used for accessing elements at a specified index in a sequence while handling out-of-bounds scenarios gracefully. By understanding and using the elementAtOrElse function, you can efficiently retrieve elements based on their positions in your Kotlin applications, ensuring that you can access specific elements according to your requirements while providing default values for out-of-bounds indices.