The distinctBy function in Kotlin is used to return a sequence containing only distinct elements from the original sequence based on a specified selector function. It is part of the Kotlin standard library and allows you to filter out duplicate elements based on custom criteria.
Table of Contents
- Introduction
distinctByFunction Syntax- Understanding
distinctBy - Examples
- Basic Usage
- Distinct Strings by Length
- Using
distinctBywith Custom Objects - Chaining
distinctBywith Other Functions
- Real-World Use Case
- Conclusion
Introduction
The distinctBy function allows you to filter out duplicate elements from a sequence based on a custom selector function. This is useful for scenarios where you need to ensure that a sequence contains no duplicate elements according to specific criteria, enabling cleaner and more accurate data processing.
distinctBy Function Syntax
The syntax for the distinctBy function is as follows:
fun <T, K> Sequence<T>.distinctBy(selector: (T) -> K): Sequence<T>
Parameters:
selector: A lambda function that defines the criteria for determining distinct elements.
Returns:
- A sequence containing only distinct elements from the original sequence based on the specified criteria.
Understanding distinctBy
The distinctBy function works by applying the selector function to each element of a sequence and filtering out duplicates based on the value returned by the selector. The resulting sequence contains only unique elements according to the specified criteria, preserving their original order.
Examples
Basic Usage
To demonstrate the basic usage of distinctBy, we will create a sequence of integers and filter out duplicates based on their absolute values.
Example
fun main() {
val numbers = sequenceOf(-3, 2, -5, 3, -2, 1)
val distinctNumbers = numbers.distinctBy { it.absoluteValue }
println(distinctNumbers.toList()) // Output: [-3, 2, -5, 1]
}
Output:
[-3, 2, -5, 1]
Distinct Strings by Length
This example shows how to filter out duplicate strings from a sequence based on their length.
Example
fun main() {
val names = sequenceOf("Arjun", "Bhaskar", "Chitra", "Deepak", "Esha", "Anu")
val distinctNames = names.distinctBy { it.length }
println(distinctNames.toList()) // Output: [Arjun, Bhaskar, Deepak, Esha]
}
Output:
[Arjun, Bhaskar, Deepak, Esha]
Using distinctBy with Custom Objects
You can use the distinctBy function to filter out duplicate custom objects based on their properties.
Example
data class Person(val name: String, val age: Int)
fun main() {
val people = sequenceOf(
Person("Arjun", 25),
Person("Bhaskar", 30),
Person("Chitra", 25),
Person("Deepak", 28),
Person("Esha", 26),
Person("Arjun", 25)
)
val distinctPeople = people.distinctBy { it.name }
println(distinctPeople.toList()) // Output: [Person(name=Arjun, age=25), Person(name=Bhaskar, age=30), Person(name=Chitra, age=25), Person(name=Deepak, age=28), Person(name=Esha, age=26)]
}
Output:
[Person(name=Arjun, age=25), Person(name=Bhaskar, age=30), Person(name=Chitra, age=25), Person(name=Deepak, age=28), Person(name=Esha, age=26)]
Chaining distinctBy with Other Functions
The distinctBy function can be chained with other sequence functions to perform more complex operations.
Example
fun main() {
val numbers = sequenceOf(1, 2, 2, 3, 4, 4, 5)
val result = numbers.map { it * 2 }
.distinctBy { it % 3 }
.filter { it > 5 }
println(result.toList()) // Output: [6, 8]
}
Output:
[6, 8]
Real-World Use Case
Filtering Unique Products by Name
In real-world applications, the distinctBy function can be used to filter out duplicate products based on their name or other attributes.
Example
data class Product(val name: String, val price: Double)
fun main() {
val products = sequenceOf(
Product("Laptop", 999.99),
Product("Smartphone", 499.99),
Product("Laptop", 899.99),
Product("Tablet", 299.99),
Product("Smartwatch", 199.99),
Product("Tablet", 399.99)
)
val distinctProducts = products.distinctBy { it.name }
println(distinctProducts.toList()) // Output: [Product(name=Laptop, price=999.99), Product(name=Smartphone, price=499.99), Product(name=Tablet, price=299.99), Product(name=Smartwatch, price=199.99)]
}
Output:
[Product(name=Laptop, price=999.99), Product(name=Smartphone, price=499.99), Product(name=Tablet, price=299.99), Product(name=Smartwatch, price=199.99)]
Conclusion
The distinctBy function in Kotlin provides used for filtering out duplicate elements in a sequence based on custom criteria. By understanding and using the distinctBy function, you can efficiently manage and process data in your Kotlin applications, ensuring that sequences contain only unique elements according to the specified criteria.