none
所在位置是kotlin.collections.none
,其相关用法介绍如下。
用法一
如果数组没有元素,则返回 true
。
例子:
import kotlin.test.*
fun main(args: Array<String>) {
//sampleStart
val emptyList = emptyList<Int>()
println("emptyList.none() is ${emptyList.none()}") // true
val nonEmptyList = listOf("one", "two", "three")
println("nonEmptyList.none() is ${nonEmptyList.none()}") // false
//sampleEnd
}
输出:
emptyList.none() is true nonEmptyList.none() is false
用法二
如果没有元素与给定的 predicate 匹配,则返回 true
。
例子:
import kotlin.test.*
fun main(args: Array<String>) {
//sampleStart
val isEven: (Int) -> Boolean = { it % 2 == 0 }
val zeroToTen = 0..10
println("zeroToTen.none { isEven(it) } is ${zeroToTen.none { isEven(it) }}") // false
println("zeroToTen.none(isEven) is ${zeroToTen.none(isEven)}") // false
val odds = zeroToTen.map { it * 2 + 1 }
println("odds.none { isEven(it) } is ${odds.none { isEven(it) }}") // true
val emptyList = emptyList<Int>()
println("emptyList.none { true } is ${emptyList.none { true }}") // true
//sampleEnd
}
输出:
zeroToTen.none { isEven(it) } is false zeroToTen.none(isEven) is false odds.none { isEven(it) } is true emptyList.none { true } is true
用法三
如果集合没有元素,则返回 true
。
例子:
import kotlin.test.*
fun main(args: Array<String>) {
//sampleStart
val emptyList = emptyList<Int>()
println("emptyList.none() is ${emptyList.none()}") // true
val nonEmptyList = listOf("one", "two", "three")
println("nonEmptyList.none() is ${nonEmptyList.none()}") // false
//sampleEnd
}
输出:
emptyList.none() is true nonEmptyList.none() is false
用法四
如果Map没有条目,则返回 true
。
例子:
import kotlin.test.*
fun main(args: Array<String>) {
//sampleStart
val emptyList = emptyList<Int>()
println("emptyList.none() is ${emptyList.none()}") // true
val nonEmptyList = listOf("one", "two", "three")
println("nonEmptyList.none() is ${nonEmptyList.none()}") // false
//sampleEnd
}
输出:
emptyList.none() is true nonEmptyList.none() is false
用法五
如果没有条目与给定的 predicate 匹配,则返回 true
。
例子:
import kotlin.test.*
fun main(args: Array<String>) {
//sampleStart
val isEven: (Int) -> Boolean = { it % 2 == 0 }
val zeroToTen = 0..10
println("zeroToTen.none { isEven(it) } is ${zeroToTen.none { isEven(it) }}") // false
println("zeroToTen.none(isEven) is ${zeroToTen.none(isEven)}") // false
val odds = zeroToTen.map { it * 2 + 1 }
println("odds.none { isEven(it) } is ${odds.none { isEven(it) }}") // true
val emptyList = emptyList<Int>()
println("emptyList.none { true } is ${emptyList.none { true }}") // true
//sampleEnd
}
输出:
zeroToTen.none { isEven(it) } is false zeroToTen.none(isEven) is false odds.none { isEven(it) } is true emptyList.none { true } is true
相关用法
- Kotlin associateBy用法及代码示例
- Kotlin all用法及代码示例
- Kotlin map用法及代码示例
- Kotlin filterNot用法及代码示例
- Kotlin reduceRight用法及代码示例
- Kotlin Random.Default用法及代码示例
- Kotlin Byte.inc用法及代码示例
- Kotlin getValue用法及代码示例
- Kotlin Double.dec用法及代码示例
- Kotlin windowedSequence用法及代码示例
- Kotlin contentToString用法及代码示例
- Kotlin groupByTo用法及代码示例
- Kotlin commonPrefixWith用法及代码示例
- Kotlin MatchResult.Destructured用法及代码示例
- Kotlin Delegates.notNull用法及代码示例
- Kotlin ifBlank用法及代码示例
- Kotlin filterNotTo用法及代码示例
- Kotlin getOrPut用法及代码示例
- Kotlin Triple.<init>用法及代码示例
- Kotlin Duration.toString用法及代码示例
- Kotlin windowed用法及代码示例
- Kotlin indexOf用法及代码示例
- Kotlin reduceIndexed用法及代码示例
- Kotlin filterKeys用法及代码示例
- Kotlin Long.inc用法及代码示例
注:本文由纯净天空筛选整理自kotlinlang.org大神的英文原创作品 kotlin.collections.none。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。