5.3.10 生产操作符
zip(other: Iterable<R>): List<Pair<T, R>>
两个集合按照下标配对,组合成的每个Pair作为新的List集合中的元素,并返回。
如果两个集合长度不一样,取短的长度。
代码示例
1. >>> val list1 = listOf(1,2,3)
2. >>> val list2 = listOf(4,5,6,7)
3. >>> val list3 = listOf("x","y","z")
4. >>> list1.zip(list3)
5. [(1, x), (2, y), (3, z)]
6. >>> list3.zip(list1)
7. [(x, 1), (y, 2), (z, 3)]
8. >>> list2.zip(list3)
9. [(4, x), (5, y), (6, z)] // 取短的长度
10. >>> list3.zip(list2)
11. [(x, 4), (y, 5), (z, 6)]
12. >>> list1.zip(listOf<Int>())
13. []
这个zip函数的定义如下:
1. public infix fun <T, R> Iterable<T>.zip(other: Iterable<R>): List<Pair<T, R>> {
2. return zip(other) { t1, t2 -> t1 to t2 }
3. }
我们可以看出,其内部是调用了zip(other) { t1, t2 -> t1 to t2 }。这个函数定义如下:
1. public inline fun <T, R, V> Iterable<T>.zip(other: Iterable<R>, transform: (a: T, b: R) -> V): List<V> {
2. val first = iterator()
3. val second = other.iterator()
4. val list = ArrayList<V>(minOf(collectionSizeOrDefault(10), other.collectionSizeOrDefault(10)))
5. while (first.hasNext() && second.hasNext()) {
6. list.add(transform(first.next(), second.next()))
7. }
8. return list
9. }
依次取两个集合相同索引的元素,使用提供的转换函数transform得到映射之后的值,作为元素组成一个新的List,并返回该List。列表的长度取两个集合中最短的。
代码示例
1. >>> val list1 = listOf(1,2,3)
2. >>> val list2 = listOf(4,5,6,7)
3. >>> val list3 = listOf("x","y","z")
4. >>> list1.zip(list3, {t1,t2 -> t2+t1})
5. [x1, y2, z3]
6. >>> list1.zip(list2, {t1,t2 -> t1*t2})
7. [4, 10, 18]
8. ```#### `unzip(): Pair<List<T>, List<R>>`
10. 首先这个函数作用在元素是Pair的集合类上。依次取各个Pair元素的first, second值,分别放到List<T>、List<R>中,然后返回一个first为List<T>,second为List<R>的大的Pair。
12. 函数定义
14. ```kotlin
15. public fun <T, R> Iterable<Pair<T, R>>.unzip(): Pair<List<T>, List<R>> {
16. val expectedSize = collectionSizeOrDefault(10)
17. val listT = ArrayList<T>(expectedSize)
18. val listR = ArrayList<R>(expectedSize)
19. for (pair in this) {
20. listT.add(pair.first)
21. listR.add(pair.second)
22. }
23. return listT to listR
24. }
看到这里,仍然有点抽象,我们直接看代码示例:
1. >>> val listPair = listOf(Pair(1,2),Pair(3,4),Pair(5,6))
2. >>> listPair
3. [(1, 2), (3, 4), (5, 6)]
4. >>> listPair.unzip()
5. ([1, 3, 5], [2, 4, 6])
6. ```#### `partition(predicate: (T) -> Boolean): Pair<List<T>, List<T>>`
8. 根据判断条件是否成立,将集合拆分成两个子集合组成的 Pair。我们可以直接看函数的定义来更加清晰的理解这个函数的功能:
10. ```kotlin
11. public inline fun <T> Iterable<T>.partition(predicate: (T) -> Boolean): Pair<List<T>, List<T>> {
12. val first = ArrayList<T>()
13. val second = ArrayList<T>()
14. for (element in this) {
15. if (predicate(element)) {
16. first.add(element)
17. } else {
18. second.add(element)
19. }
20. }
21. return Pair(first, second)
22. }
我们可以看出,这是一个内联函数。
代码示例
1. >>> val list = listOf(1,2,3,4,5,6,7,8,9)
2. >>> list
3. [1, 2, 3, 4, 5, 6, 7, 8, 9]
4. >>> list.partition({it>5})
5. ([6, 7, 8, 9], [1, 2, 3, 4, 5])
6. ```#### `plus(elements: Iterable<T>): List<T>`
8. 合并两个List。
10. 函数定义
12. ```kotlin
13. public operator fun <T> Iterable<T>.plus(elements: Iterable<T>): List<T> {
14. if (this is Collection) return this.plus(elements)
15. val result = ArrayList<T>()
16. result.addAll(this)
17. result.addAll(elements)
18. return result
19. }
我们可以看出,这是一个操作符函数。可以用”+”替代 。
代码示例
1. >>> val list1 = listOf(1,2,3)
2. >>> val list2 = listOf(4,5)
3. >>> list1.plus(list2)
4. [1, 2, 3, 4, 5]
5. >>> list1+list2
6. [1, 2, 3, 4, 5]
关于plus函数还有以下的重载函数:
1. plus(element: T): List<T>
2. plus(elements: Array<out T>): List<T>
3. plus(elements: Sequence<T>): List<T>
等。#### plusElement(element: T): List<T>
在集合中添加一个元素。 函数定义
1. @kotlin.internal.InlineOnly
2. public inline fun <T> Iterable<T>.plusElement(element: T): List<T> {
3. return plus(element)
4. }
我们可以看出,这个函数内部是直接调用的plus(element: T): List<T>。
代码示例
1. >>> list1 + 10
2. [1, 2, 3, 10]
3. >>> list1.plusElement(10)
4. [1, 2, 3, 10]
5. >>> list1.plus(10)
6. [1, 2, 3, 10]
