Kotlin List none()
Syntax & Examples
Syntax of List.none()
There are 2 variations for the syntax of List.none() extension function. They are:
1.
fun <T> Iterable<T>.none(): Boolean
This extension function returns true if the collection has no elements.
2.
fun <T> Iterable<T>.none(predicate: (T) -> Boolean): Boolean
This extension function returns true if no elements match the given predicate.
✐ Examples
1 Example
In this example,
- We create a list of integers named
list
containing elements1, 2, 3, 4, 5
. - We check if the list has no elements using the
none()
function. - The result, which is a Boolean value indicating if the list has no elements, is stored in
noneResult
. - The value of
noneResult
is printed to standard output using println statement.
Kotlin Program
fun main(args: Array<String>) {
val list = listOf(1, 2, 3, 4, 5)
val noneResult = list.none()
println(noneResult)
}
Output
false
2 Example
In this example,
- We create a list of characters named
list
containing elements'a', 'e', 'i', 'o', 'u'
. - We check if none of the elements are uppercase letters using the
none()
function with a predicate. - The result, which is a Boolean value indicating if none of the elements match the predicate, is stored in
noneResult
. - The value of
noneResult
is printed to standard output using println statement.
Kotlin Program
fun main(args: Array<String>) {
val list = listOf('a', 'e', 'i', 'o', 'u')
val noneResult = list.none { it.isUpperCase() }
println(noneResult)
}
Output
true
3 Example
In this example,
- We create a list of strings named
list
containing elements"apple", "banana", "cherry"
. - We check if none of the strings have a length greater than 6 characters using the
none()
function with a predicate. - The result, which is a Boolean value indicating if none of the elements match the predicate, is stored in
noneResult
. - The value of
noneResult
is printed to standard output using println statement.
Kotlin Program
fun main(args: Array<String>) {
val list = listOf("apple", "banana", "cherry")
val noneResult = list.none { it.length > 6 }
println(noneResult)
}
Output
true
Summary
In this Kotlin tutorial, we learned about none() extension function of List: the syntax and few working examples with output and detailed explanation for each example.