The distinct function in Kotlin is used to return a sequence containing only distinct elements from the original sequence. It is part of the Kotlin standard library and allows you to filter out duplicate elements, ensuring that each element in the sequence is unique.
Table of Contents
- Introduction
distinctFunction Syntax- Understanding
distinct - Examples
- Basic Usage
- Distinct Strings
- Using
distinctwith Custom Objects - Chaining
distinctwith Other Functions
- Real-World Use Case
- Conclusion
Introduction
The distinct function allows you to filter out duplicate elements from a sequence, leaving only unique elements. This is useful for scenarios where you need to ensure that a sequence contains no duplicate elements, enabling cleaner and more accurate data processing.
distinct Function Syntax
The syntax for the distinct function is as follows:
fun <T> Sequence<T>.distinct(): Sequence<T>
Returns:
- A sequence containing only distinct elements from the original sequence.
Understanding distinct
The distinct function works by comparing the elements of a sequence and filtering out duplicates. The resulting sequence contains only unique elements, preserving their original order.
Examples
Basic Usage
To demonstrate the basic usage of distinct, we will create a sequence of integers with duplicate values and filter out the duplicates.
Example
fun main() {
val numbers = sequenceOf(1, 2, 2, 3, 4, 4, 5)
val distinctNumbers = numbers.distinct()
println(distinctNumbers.toList()) // Output: [1, 2, 3, 4, 5]
}
Output:
[1, 2, 3, 4, 5]
Distinct Strings
This example shows how to filter out duplicate strings from a sequence.
Example
fun main() {
val names = sequenceOf("Arjun", "Bhaskar", "Arjun", "Chitra", "Deepak", "Chitra", "Esha")
val distinctNames = names.distinct()
println(distinctNames.toList()) // Output: [Arjun, Bhaskar, Chitra, Deepak, Esha]
}
Output:
[Arjun, Bhaskar, Chitra, Deepak, Esha]
Using distinct with Custom Objects
You can use the distinct 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("Arjun", 25),
Person("Chitra", 22),
Person("Deepak", 28),
Person("Chitra", 22),
Person("Esha", 26)
)
val distinctPeople = people.distinct()
println(distinctPeople.toList()) // Output: [Person(name=Arjun, age=25), Person(name=Bhaskar, age=30), Person(name=Chitra, age=22), 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=22), Person(name=Deepak, age=28), Person(name=Esha, age=26)]
Chaining distinct with Other Functions
The distinct 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 }
.distinct()
.filter { it > 5 }
println(result.toList()) // Output: [6, 8, 10]
}
Output:
[6, 8, 10]
Real-World Use Case
Filtering Unique Products
In real-world applications, the distinct function can be used to filter out duplicate products based on their properties, such as name and price.
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", 999.99),
Product("Tablet", 299.99),
Product("Smartwatch", 199.99),
Product("Tablet", 299.99)
)
val distinctProducts = products.distinct()
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 distinct function in Kotlin provides used for filtering out duplicate elements in a sequence. By understanding and using the distinct function, you can efficiently manage and process data in your Kotlin applications, ensuring that sequences contain only unique elements.