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

用法一

fun <T, R> Array<out Pair<T, R>>.unzip(): Pair<List<T>, List<R>>

返回一对列表,其中 first 列表是根据该数组中每对的第一个值构建的,second 列表是根据该数组中每对的第二个值构建的。

例子:

import kotlin.test.*

fun main(args: Array<String>) {
//sampleStart
val array = arrayOf(1 to 'a', 2 to 'b', 3 to 'c')
println(array.unzip()) // ([1, 2, 3], [a, b, c])
//sampleEnd
}

输出:

([1, 2, 3], [a, b, c])

用法二

fun <T, R> Iterable<Pair<T, R>>.unzip(): Pair<List<T>, List<R>>

返回一对列表,其中 first 列表是根据此集合中每对的第一个值构建的,second 列表是根据此集合中每对的第二个值构建的。

例子:



fun main(args: Array<String>) {
//sampleStart
val list = listOf(1 to 'a', 2 to 'b', 3 to 'c')
println(list.unzip()) // ([1, 2, 3], [a, b, c])
//sampleEnd
}

输出:

([1, 2, 3], [a, b, c])