filterNotTo
所在位置是kotlin.collections.filterNotTo
,其相关用法介绍如下。
用法一
inline fun <T, C : MutableCollection<in T>> Array<out T>.filterNotTo(
destination: C,
predicate: (T) -> Boolean
): C
inline fun <C : MutableCollection<in Byte>> ByteArray.filterNotTo(
destination: C,
predicate: (Byte) -> Boolean
): C
inline fun <C : MutableCollection<in Short>> ShortArray.filterNotTo(
destination: C,
predicate: (Short) -> Boolean
): C
inline fun <C : MutableCollection<in Int>> IntArray.filterNotTo(
destination: C,
predicate: (Int) -> Boolean
): C
inline fun <C : MutableCollection<in Long>> LongArray.filterNotTo(
destination: C,
predicate: (Long) -> Boolean
): C
inline fun <C : MutableCollection<in Float>> FloatArray.filterNotTo(
destination: C,
predicate: (Float) -> Boolean
): C
inline fun <C : MutableCollection<in Double>> DoubleArray.filterNotTo(
destination: C,
predicate: (Double) -> Boolean
): C
inline fun <C : MutableCollection<in Boolean>> BooleanArray.filterNotTo(
destination: C,
predicate: (Boolean) -> Boolean
): C
inline fun <C : MutableCollection<in Char>> CharArray.filterNotTo(
destination: C,
predicate: (Char) -> Boolean
): C
inline fun <T, C : MutableCollection<in T>> Iterable<T>.filterNotTo(
destination: C,
predicate: (T) -> Boolean
): C
@ExperimentalUnsignedTypes inline fun <C : MutableCollection<in UInt>> UIntArray.filterNotTo(
destination: C,
predicate: (UInt) -> Boolean
): C
@ExperimentalUnsignedTypes inline fun <C : MutableCollection<in ULong>> ULongArray.filterNotTo(
destination: C,
predicate: (ULong) -> Boolean
): C
@ExperimentalUnsignedTypes inline fun <C : MutableCollection<in UByte>> UByteArray.filterNotTo(
destination: C,
predicate: (UByte) -> Boolean
): C
@ExperimentalUnsignedTypes inline fun <C : MutableCollection<in UShort>> UShortArray.filterNotTo(
destination: C,
predicate: (UShort) -> Boolean
): C
将与给定 predicate 不匹配的所有元素附加到给定 destination 。
例子:
import kotlin.test.*
fun main(args: Array<String>) {
//sampleStart
val numbers: List<Int> = listOf(1, 2, 3, 4, 5, 6, 7)
val evenNumbers = mutableListOf<Int>()
val notMultiplesOf3 = mutableListOf<Int>()
println(evenNumbers) // []
numbers.filterTo(evenNumbers) { it % 2 == 0 }
numbers.filterNotTo(notMultiplesOf3) { number -> number % 3 == 0 }
println(evenNumbers) // [2, 4, 6]
println(notMultiplesOf3) // [1, 2, 4, 5, 7]
//sampleEnd
}
输出:
[] [2, 4, 6] [1, 2, 4, 5, 7]
用法二
inline fun <K, V, M : MutableMap<in K, in V>> Map<out K, V>.filterNotTo(
destination: M,
predicate: (Entry<K, V>) -> Boolean
): M
将所有与给定 predicate 不匹配的条目追加到给定 destination 中。
例子:
import kotlin.test.*
import java.util.*
fun main(args: Array<String>) {
//sampleStart
val originalMap = mapOf("key1" to 1, "key2" to 2, "key3" to 3)
val destinationMap = mutableMapOf("key40" to 40, "key50" to 50)
val filteredMap = originalMap.filterNotTo(destinationMap) { it.value < 3 }
//destination map instance has been updated
println("destinationMap === filteredMap is ${destinationMap === filteredMap}") // true
println(destinationMap) // {key40=40, key50=50, key3=3}
// original map has not changed
println(originalMap) // {key1=1, key2=2, key3=3}
val anotherDestinationMap = mutableMapOf("key40" to 40, "key50" to 50)
val matchAllPredicate: ((Map.Entry<String, Int>)) -> Boolean = { it.value > 0 }
val filteredMapWithEverythingMatched = originalMap.filterNotTo(anotherDestinationMap, matchAllPredicate)
println(filteredMapWithEverythingMatched) // {key40=40, key50=50}
//sampleEnd
}
输出:
destinationMap === filteredMap is true {key40=40, key50=50, key3=3} {key1=1, key2=2, key3=3} {key40=40, key50=50}
返回目的地Map。
相关用法
- Kotlin filterNot用法及代码示例
- Kotlin filterNotNull用法及代码示例
- Kotlin filterNotNullTo用法及代码示例
- Kotlin filterKeys用法及代码示例
- Kotlin filterTo用法及代码示例
- Kotlin filterIsInstance用法及代码示例
- Kotlin filterIsInstanceTo用法及代码示例
- Kotlin filterIndexedTo用法及代码示例
- Kotlin filterValues用法及代码示例
- Kotlin filter用法及代码示例
- Kotlin filterIndexed用法及代码示例
- Kotlin findLast用法及代码示例
- Kotlin find用法及代码示例
- Kotlin firstNotNullOf用法及代码示例
- Kotlin firstNotNullOfOrNull用法及代码示例
- Kotlin flatten用法及代码示例
- Kotlin forEach用法及代码示例
- Kotlin flatMap用法及代码示例
- Kotlin fold用法及代码示例
- Kotlin foldTo用法及代码示例
- Kotlin flatMapIndexed用法及代码示例
- Kotlin associateBy用法及代码示例
- Kotlin all用法及代码示例
- Kotlin map用法及代码示例
- Kotlin reduceRight用法及代码示例
注:本文由纯净天空筛选整理自kotlinlang.org大神的英文原创作品 kotlin.collections.filterNotTo。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。