filterTo

Common
JVM
JS
Native
1.0
inline fun <T, C : MutableCollection<in T>> Array<out T>.filterTo(
    destination: C,
    predicate: (T) -> Boolean
): C
inline fun <C : MutableCollection<in Byte>> ByteArray.filterTo(
    destination: C,
    predicate: (Byte) -> Boolean
): C
inline fun <C : MutableCollection<in Short>> ShortArray.filterTo(
    destination: C,
    predicate: (Short) -> Boolean
): C
inline fun <C : MutableCollection<in Int>> IntArray.filterTo(
    destination: C,
    predicate: (Int) -> Boolean
): C
inline fun <C : MutableCollection<in Long>> LongArray.filterTo(
    destination: C,
    predicate: (Long) -> Boolean
): C
inline fun <C : MutableCollection<in Float>> FloatArray.filterTo(
    destination: C,
    predicate: (Float) -> Boolean
): C
inline fun <C : MutableCollection<in Double>> DoubleArray.filterTo(
    destination: C,
    predicate: (Double) -> Boolean
): C
inline fun <C : MutableCollection<in Boolean>> BooleanArray.filterTo(
    destination: C,
    predicate: (Boolean) -> Boolean
): C
inline fun <C : MutableCollection<in Char>> CharArray.filterTo(
    destination: C,
    predicate: (Char) -> Boolean
): C
inline fun <T, C : MutableCollection<in T>> Iterable<T>.filterTo(
    destination: C,
    predicate: (T) -> Boolean
): C
@ExperimentalUnsignedTypes inline fun <C : MutableCollection<in UInt>> UIntArray.filterTo(
    destination: C,
    predicate: (UInt) -> Boolean
): C
@ExperimentalUnsignedTypes inline fun <C : MutableCollection<in ULong>> ULongArray.filterTo(
    destination: C,
    predicate: (ULong) -> Boolean
): C
@ExperimentalUnsignedTypes inline fun <C : MutableCollection<in UByte>> UByteArray.filterTo(
    destination: C,
    predicate: (UByte) -> Boolean
): C
@ExperimentalUnsignedTypes inline fun <C : MutableCollection<in UShort>> UShortArray.filterTo(
    destination: C,
    predicate: (UShort) -> Boolean
): C

Appends all elements matching the given predicate to the given 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
}
Common
JVM
JS
Native
1.0
inline fun <K, V, M : MutableMap<in K, in V>> Map<out K, V>.filterTo(
    destination: M,
    predicate: (Entry<K, V>) -> Boolean
): M

Appends all entries matching the given predicate into the mutable map given as destination parameter.

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.filterTo(destinationMap) { it.value < 3 }

//destination map is updated with filtered items from the original map
println("destinationMap === filteredMap is ${destinationMap === filteredMap}") // true
println(destinationMap) // {key40=40, key50=50, key1=1, key2=2}
// original map has not changed
println(originalMap) // {key1=1, key2=2, key3=3}

val nonMatchingPredicate: ((Map.Entry<String, Int>)) -> Boolean = { it.value == 0 }
val anotherDestinationMap = mutableMapOf("key40" to 40, "key50" to 50)
val filteredMapWithNothingMatched = originalMap.filterTo(anotherDestinationMap, nonMatchingPredicate)
println(filteredMapWithNothingMatched) // {key40=40, key50=50}
//sampleEnd
}

Return the destination map.