The all function in Kotlin is used to check if all elements in a sequence match a given predicate. It is part of the Kotlin standard library and provides a way to verify that every element in the sequence satisfies a specific condition.
Table of Contents
- Introduction
allFunction Syntax- Understanding
all - Examples
- Basic Usage
- Checking All Elements in a Sequence of Strings
- Using
allwith Custom Objects - Chaining
allwith Other Functions
- Real-World Use Case
- Conclusion
Introduction
The all function allows you to verify if all elements in a sequence match a given predicate. This is useful for scenarios where you need to ensure that every element in a collection meets certain criteria, such as validating input data or checking constraints.
all Function Syntax
The syntax for the all function is as follows:
fun <T> Sequence<T>.all(predicate: (T) -> Boolean): Boolean
Parameters:
predicate: A lambda function that defines the condition each element in the sequence must satisfy.
Returns:
trueif all elements in the sequence match the given predicate,falseotherwise.
Understanding all
The all function works by iterating through the sequence and applying the predicate to each element. If all elements satisfy the predicate, the function returns true. If any element does not satisfy the predicate, the function returns false.
Examples
Basic Usage
To demonstrate the basic usage of all, we will create a sequence of integers and check if all elements are positive.
Example
fun main() {
val numbers = sequenceOf(1, 2, 3, 4, 5)
val allPositive = numbers.all { it > 0 }
println(allPositive) // Output: true
}
Output:
true
Checking All Elements in a Sequence of Strings
This example shows how to check if all strings in a sequence have a length greater than 3.
Example
fun main() {
val names = sequenceOf("Arjun", "Bhaskar", "Chitra", "Deepak", "Esha")
val allLongNames = names.all { it.length > 3 }
println(allLongNames) // Output: true
}
Output:
true
Using all with Custom Objects
You can use the all function to check if all custom objects in a sequence satisfy a specific condition.
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 allAdults = people.all { it.age >= 18 }
println(allAdults) // Output: true
}
Output:
true
Chaining all with Other Functions
The all function can be chained with other sequence functions to perform more complex operations before checking the condition.
Example
fun main() {
val numbers = sequenceOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
val allEvenNumbersGreaterThanFive = numbers.filter { it > 5 }
.all { it % 2 == 0 }
println(allEvenNumbersGreaterThanFive) // Output: false
}
Output:
false
Real-World Use Case
Validating Product Prices
In real-world applications, the all function can be used to validate that all products in a sequence have prices greater than a certain threshold.
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 allExpensiveProducts = products.all { it.price > 100 }
println(allExpensiveProducts) // Output: true
}
Output:
true
Conclusion
The all function in Kotlin provides used for checking if all elements in a sequence match a given predicate. By understanding and using the all function, you can efficiently validate and process data in your Kotlin applications, ensuring that all elements meet specific conditions according to your requirements.