all所在位置是kotlin.collections.all,其相关用法介绍如下。

用法一

inline fun <T> Array<out T>.all(
    predicate: (T) -> Boolean
): Boolean
inline fun ByteArray.all(
    predicate: (Byte) -> Boolean
): Boolean
inline fun ShortArray.all(
    predicate: (Short) -> Boolean
): Boolean
inline fun IntArray.all(predicate: (Int) -> Boolean): Boolean
inline fun LongArray.all(
    predicate: (Long) -> Boolean
): Boolean
inline fun FloatArray.all(
    predicate: (Float) -> Boolean
): Boolean
inline fun DoubleArray.all(
    predicate: (Double) -> Boolean
): Boolean
inline fun BooleanArray.all(
    predicate: (Boolean) -> Boolean
): Boolean
inline fun CharArray.all(
    predicate: (Char) -> Boolean
): Boolean
inline fun <T> Iterable<T>.all(
    predicate: (T) -> Boolean
): Boolean
@ExperimentalUnsignedTypes inline fun UIntArray.all(
    predicate: (UInt) -> Boolean
): Boolean
@ExperimentalUnsignedTypes inline fun ULongArray.all(
    predicate: (ULong) -> Boolean
): Boolean
@ExperimentalUnsignedTypes inline fun UByteArray.all(
    predicate: (UByte) -> Boolean
): Boolean
@ExperimentalUnsignedTypes inline fun UShortArray.all(
    predicate: (UShort) -> Boolean
): Boolean

如果所有元素都与给定的 predicate 匹配,则返回 true

例子:

import kotlin.test.*

fun main(args: Array<String>) {
//sampleStart
val isEven: (Int) -> Boolean = { it % 2 == 0 }
val zeroToTen = 0..10
println("zeroToTen.all { isEven(it) } is ${zeroToTen.all { isEven(it) }}") // false
println("zeroToTen.all(isEven) is ${zeroToTen.all(isEven)}") // false

val evens = zeroToTen.map { it * 2 }
println("evens.all { isEven(it) } is ${evens.all { isEven(it) }}") // true

val emptyList = emptyList<Int>()
println("emptyList.all { false } is ${emptyList.all { false }}") // true
//sampleEnd
}

输出:

zeroToTen.all { isEven(it) } is false
zeroToTen.all(isEven) is false
evens.all { isEven(it) } is true
emptyList.all { false } is true

用法二

inline fun <K, V> Map<out K, V>.all(
    predicate: (Entry<K, V>) -> Boolean
): Boolean

如果所有条目都与给定的 predicate 匹配,则返回 true

例子:

import kotlin.test.*

fun main(args: Array<String>) {
//sampleStart
val isEven: (Int) -> Boolean = { it % 2 == 0 }
val zeroToTen = 0..10
println("zeroToTen.all { isEven(it) } is ${zeroToTen.all { isEven(it) }}") // false
println("zeroToTen.all(isEven) is ${zeroToTen.all(isEven)}") // false

val evens = zeroToTen.map { it * 2 }
println("evens.all { isEven(it) } is ${evens.all { isEven(it) }}") // true

val emptyList = emptyList<Int>()
println("emptyList.all { false } is ${emptyList.all { false }}") // true
//sampleEnd
}

输出:

zeroToTen.all { isEven(it) } is false
zeroToTen.all(isEven) is false
evens.all { isEven(it) } is true
emptyList.all { false } is true