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

用法:

inline fun <T, C : MutableCollection<in T>> Sequence<T>.filterTo(
    destination: C, 
    predicate: (T) -> Boolean
): C

将与给定 predicate 匹配的所有元素附加到给定 destination

操作是 terminal

例子:

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]