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。