- Kotlin - Home
- Kotlin - Overview
- Kotlin - Environment Setup
- Kotlin - Architecture
- Kotlin - Basic Syntax
- Kotlin - Comments
- Kotlin - Keywords
- Kotlin - Variables
- Kotlin - Data Types
- Kotlin - Operators
- Kotlin - Booleans
- Kotlin - Strings
- Kotlin - Arrays
- Kotlin - Ranges
- Kotlin - Functions
- Kotlin Control Flow
- Kotlin - Control Flow
- Kotlin - if...Else Expression
- Kotlin - When Expression
- Kotlin - For Loop
- Kotlin - While Loop
- Kotlin - Break and Continue
- Kotlin Collections
- Kotlin - Collections
- Kotlin - Lists
- Kotlin - Sets
- Kotlin - Maps
- Kotlin Objects and Classes
- Kotlin - Class and Objects
- Kotlin - Constructors
- Kotlin - Inheritance
- Kotlin - Abstract Classes
- Kotlin - Interface
- Kotlin - Visibility Control
- Kotlin - Extension
- Kotlin - Data Classes
- Kotlin - Sealed Class
- Kotlin - Generics
- Kotlin - Delegation
- Kotlin - Destructuring Declarations
- Kotlin - Exception Handling
Kotlin Array - all() Function
The Kotlin array all() function is used to check all the elements satisfy the given predicate. If they do, it returns true; otherwise, it returns false.
If the array does not contains any elements, the function returns true because there are no elements in it that do not match the predicate.
An important use case of this function is to check whether an array is consistent with the same type of objects in it.
Syntax
Following is the syntax of Kotlin array all() function −
inline fun <T> Array<out T>.all(
predicate: (T) -> Boolean
): Boolean
Parameters
This function accepts apredicateas a parameter, which is a lambda function that takes an element of type T.
Return value
This function returns a boolean value: true if all elements in the array satisfy the predicate; otherwise, false.
Example 1
Following is the basic example, we use the all() function to check all the element is of same type or not −
fun main(args: Array<String>) {
var array = Array(10) { i -> i}
var result = array.all() {
i -> i is Int
}
if (result == true){
println("The array is of type integer")
}
else{
println("The array is not of type integer")
}
}
Output
Following is the output −
The array is of type integer
Example 2
Now, let's see another example. Here, we use the all() function to check all the element is positive, or even −
fun main() {
val numbers = arrayOf(1, 2, 3, 4, 5)
// Check if all elements in the array are greater than 0
val areAllPositive = numbers.all { it > 0 }
println("Are all numbers positive? $areAllPositive")
// Check if all elements in the array are even
val areAllEven = numbers.all { it % 2 == 0 }
println("Are all numbers even? $areAllEven")
}
Output
Following is the output −
Are all numbers positive? true Are all numbers even? false
Example 3
The example below checks whether the length of each element in a string array is at-least 1 −
fun main(args: Array<String>) {
var array = arrayOf("Hi", "", "Sample", " Bye")
var result = array.all {
i -> i.length >= 1
}
if (result == true){
println("All the values in the array have their string length of more than or equal to 1")
}
else{
println("All the values in the array don't have their string length of more than or equal to 1")
}
}
Output
Following is the output −
All the values in the array don't have their string length of more than or equal to 1