Kotlin Sequence elementAt Function

The elementAt function in Kotlin is used to retrieve an element at a specified index in a sequence. It is part of the Kotlin standard library and provides a way to access elements based on their position within a sequence.

Table of Contents

  1. Introduction
  2. elementAt Function Syntax
  3. Understanding elementAt
  4. Examples
    • Basic Usage
    • Retrieving an Element in a Sequence of Strings
    • Using elementAt with Custom Objects
    • Chaining elementAt with Other Functions
  5. Real-World Use Case
  6. Conclusion

Introduction

The elementAt function allows you to access an element at a specified index in a sequence. This is useful for scenarios where you need to retrieve a specific element based on its position, such as finding the nth element in a list.

elementAt Function Syntax

The syntax for the elementAt function is as follows:

fun <T> Sequence<T>.elementAt(index: Int): T

Parameters:

  • index: The index of the element to retrieve from the sequence.

Returns:

  • The element at the specified index.

Throws:

  • IndexOutOfBoundsException if the index is out of the range of the sequence.

Understanding elementAt

The elementAt function works by iterating through the sequence until it reaches the specified index, then returns the element at that index. If the index is out of the range of the sequence, it throws an IndexOutOfBoundsException.

Examples

Basic Usage

To demonstrate the basic usage of elementAt, we will create a sequence of integers and retrieve the element at a specific index.

Example

fun main() {
    val numbers = sequenceOf(1, 2, 3, 4, 5)
    val element = numbers.elementAt(2)
    println(element) // Output: 3
}

Output:

3

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.

Example

fun main() {
    val names = sequenceOf("Arjun", "Bhaskar", "Chitra", "Deepak", "Esha")
    val element = names.elementAt(3)
    println(element) // Output: Deepak
}

Output:

Deepak

Using elementAt with Custom Objects

You can use the elementAt function to retrieve an element at a specific index in a sequence of custom objects.

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.elementAt(4)
    println(element) // Output: Person(name=Esha, age=26)
}

Output:

Person(name=Esha, age=26)

Chaining elementAt with Other Functions

The elementAt function can be chained with other sequence functions to perform more complex operations before retrieving the element at a specific index.

Example

fun main() {
    val numbers = sequenceOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
    val element = numbers.filter { it % 2 == 0 }
                         .elementAt(2)
    println(element) // Output: 6
}

Output:

6

Real-World Use Case

Retrieving a Specific Product

In real-world applications, the elementAt function can be used to retrieve a specific product from a sequence of products based on its position.

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.elementAt(1)
    println(element) // Output: Product(name=Smartphone, price=600.0)
}

Output:

Product(name=Smartphone, price=600.0)

Conclusion

The elementAt function in Kotlin provides used for accessing elements at a specified index in a sequence. By understanding and using the elementAt 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.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top