Kotlin Sequence indexOf Function

The indexOf function in Kotlin is used to find the index of the first occurrence of a specified element in a sequence. It is part of the Kotlin standard library and provides a way to determine the position of an element within a sequence.

Table of Contents

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

Introduction

The indexOf function allows you to find the index of the first occurrence of a specified element in a sequence. This is useful for scenarios where you need to locate the position of an element within a sequence, such as finding the position of a specific value in a list.

indexOf Function Syntax

The syntax for the indexOf function is as follows:

fun <T> Sequence<T>.indexOf(element: T): Int

Parameters:

  • element: The element to find the index of in the sequence.

Returns:

  • The index of the first occurrence of the specified element, or -1 if the element is not found.

Understanding indexOf

The indexOf function works by iterating through the sequence and comparing each element to the specified element. It returns the index of the first occurrence of the element. If the element is not found, it returns -1.

Examples

Basic Usage

To demonstrate the basic usage of indexOf, we will create a sequence of integers and find the index of a specific element.

Example

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

Output:

2

Finding the Index of an Element in a Sequence of Strings

This example shows how to find the index of a specific string in a sequence.

Example

fun main() {
    val names = sequenceOf("Arjun", "Bhaskar", "Chitra", "Deepak", "Esha")
    val index = names.indexOf("Chitra")
    println(index) // Output: 2
}

Output:

2

Using indexOf with Custom Objects

You can use the indexOf function to find the index of a specific custom object in a sequence.

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 index = people.indexOf(Person("Deepak", 28))
    println(index) // Output: 3
}

Output:

3

Chaining indexOf with Other Functions

The indexOf function can be chained with other sequence functions to perform more complex operations before finding the index of an element.

Example

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

Output:

2

Real-World Use Case

Finding the Index of a Specific Product

In real-world applications, the indexOf function can be used to find the index of a specific product in a sequence of products.

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 index = products.indexOf(Product("Tablet", 300.0))
    println(index) // Output: 2
}

Output:

2

Conclusion

The indexOf function in Kotlin provides used for finding the index of the first occurrence of a specified element in a sequence. By understanding and using the indexOf function, you can efficiently locate the position of elements within your sequences, enabling you to perform operations that depend on element positions according to your requirements.

Leave a Comment

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

Scroll to Top