Mastering Kotlin's FilterIndexed: A Comprehensive Guide
In the realm of functional programming, Kotlin's filterIndexed function stands out as a powerful tool for transforming collections. It's a variant of the filter function that provides the index of each element during iteration, opening up new possibilities for conditional filtering. Let's delve into the world of filterIndexed and explore its capabilities.
Understanding FilterIndexed
filterIndexed is an extension function defined in Kotlin's Sequence interface. It takes a predicate (a function that returns a boolean) as an argument, which is invoked with the index and value of each element in the sequence. The function returns a new sequence containing only the elements for which the predicate returns true.
Syntax
The syntax of filterIndexed is as follows:

fun Boolean): Sequence<T>
Basic Usage
Let's start with a simple example. Suppose we have a list of integers and we want to filter out all even numbers, but only if they are not in the first or last position. Here's how you can do it using filterIndexed:
val numbers = listOf(1, 2, 3, 4, 5, 6)
val filtered = numbers.filterIndexed { index, number -> number % 2 != 0 || index !in 0..1 && index !in numbers.lastIndex..numbers.lastIndex - 1 }
println(filtered) // Output: [3, 5]
Real-World Applications
filterIndexed shines when you need to consider the position of elements in a collection. Here are a few real-world use cases:
- Removing duplicates while preserving order: While Kotlin's
distinctfunction removes duplicates, it doesn't preserve the original order.filterIndexedcan help achieve this. - Processing data in chunks: You can use
filterIndexedto split a large collection into smaller chunks, allowing you to process them independently. - Validating data with position-based rules: In some cases, data validation rules might depend on the position of elements in a collection.
filterIndexedis perfect for such scenarios.
Performance Considerations
While filterIndexed is a powerful tool, it's essential to consider its performance implications. Since it provides the index of each element, it might be less efficient than filter when dealing with large collections. However, in many cases, the performance difference is negligible, and the benefits of filterIndexed outweigh the potential costs.

Alternatives and Combinations
Kotlin provides several other functions for transforming collections, such as map, flatMap, and groupBy. You can combine these functions with filterIndexed to create complex transformations. For example, you can use mapIndexed to transform each element along with its index, and then use filterIndexed to filter the results:
val numbers = listOf(1, 2, 3, 4, 5, 6)
val transformed = numbers.mapIndexed { index, number -> index * number }.filterIndexed { index, number -> number % 2 == 0 }
println(transformed) // Output: [4, 12]
Conclusion
Kotlin's filterIndexed function is a versatile tool that empowers you to filter collections based on the position of their elements. Whether you're removing duplicates, processing data in chunks, or validating data with position-based rules, filterIndexed has you covered. So go ahead, harness the power of filterIndexed, and take your Kotlin skills to the next level!























