List集合类的基本运算函数

any()判断集合至少有一个元素

这个函数定义如下:

  1. public fun <T> Iterable<T>.any(): Boolean {
  2. for (element in this) return true
  3. return false
  4. }

如果该集合至少有一个元素,返回true,否则返回false

代码示例:

  1. >>> val emptyList = listOf<Int>()
  2. >>> emptyList.any()
  3. false
  4. >>> val list1 = listOf(1)
  5. >>> list1.any()
  6. true

any(predicate: (T) -> Boolean) 判断集合中是否有满足条件的元素

这个函数定义如下:

  1. public inline fun <T> Iterable<T>.any(predicate: (T) -> Boolean): Boolean {
  2. for (element in this) if (predicate(element)) return true
  3. return false
  4. }

如果该集合中至少有一个元素匹配谓词函数参数predicate: (T) -> Boolean,返回true,否则返回false。

代码示例:

  1. >>> val list = listOf(1, 2, 3)
  2. >>> list.any() // 至少有1个元素
  3. true
  4. >>> list.any({it%2==0}) // 元素2满足{it%2==0}
  5. true
  6. >>> list.any({it>4}) // 没有元素满足{it>4}
  7. false

all(predicate: (T) -> Boolean) 判断集合中的元素是否都满足条件

函数定义:

  1. public inline fun <T> Iterable<T>.all(predicate: (T) -> Boolean): Boolean {
  2. for (element in this) if (!predicate(element)) return false
  3. return true
  4. }

当且仅当该集合中所有元素都满足条件时,返回true;否则都返回false

代码示例:

  1. >>> val list = listOf(0,2,4,6,8)
  2. >>> list.all({it%2==0})
  3. true
  4. >>> list.all({it>2})
  5. false

none()判断集合无元素

函数定义:

  1. public fun <T> Iterable<T>.none(): Boolean {
  2. for (element in this) return false
  3. return true
  4. }

如果该集合没有任何元素,返回true,否则返回false

代码示例:

  1. >>> val list = listOf<Int>()
  2. >>> list.none()
  3. true

none(predicate: (T) -> Boolean)判断集合中所有元素都不满足条件

函数定义:

  1. public inline fun <T> Iterable<T>.none(predicate: (T) -> Boolean): Boolean {
  2. for (element in this) if (predicate(element)) return false
  3. return true
  4. }

当且仅当集合中所有元素都不满足条件时返回true,否则返回false

代码示例:

  1. >>> val list = listOf(0,2,4,6,8)
  2. >>> list.none({it%2==1})
  3. true
  4. >>> list.none({it>0})
  5. false

count() 计算集合中元素的个数

函数定义:

  1. public fun <T> Iterable<T>.count(): Int {
  2. var count = 0
  3. for (element in this) count++
  4. return count
  5. }

代码示例:

  1. >>> val list = listOf(0,2,4,6,8,9)
  2. >>> list.count()
  3. 6

count(predicate: (T) -> Boolean) 计算集合中满足条件的元素的个数

函数定义:

  1. public inline fun <T> Iterable<T>.count(predicate: (T) -> Boolean): Int {
  2. var count = 0
  3. for (element in this) if (predicate(element)) count++
  4. return count
  5. }

代码示例:

  1. >>> val list = listOf(0,2,4,6,8,9)
  2. >>> list.count()
  3. 6
  4. >>> list.count({it%2==0})
  5. 5

reduce从第一项到最后一项进行累计运算

函数定义:

  1. public inline fun <S, T: S> Iterable<T>.reduce(operation: (acc: S, T) -> S): S {
  2. val iterator = this.iterator()
  3. if (!iterator.hasNext()) throw UnsupportedOperationException("Empty collection can't be reduced.")
  4. var accumulator: S = iterator.next()
  5. while (iterator.hasNext()) {
  6. accumulator = operation(accumulator, iterator.next())
  7. }
  8. return accumulator
  9. }

首先把第一个元素赋值给累加子accumulator,然后逐次向后取元素累加,新值继续赋值给累加子accumulator = operation(accumulator, iterator.next()),以此类推。最后返回累加子的值。

代码示例:

  1. >>> val list = listOf(1,2,3,4,5,6,7,8,9)
  2. >>> list.reduce({sum, next->sum+next})
  3. 45
  4. >>> list.reduce({sum, next->sum*next})
  5. 362880
  6. >>> val list = listOf("a","b","c")
  7. >>> list.reduce({total, s->total+s})
  8. abc

reduceRight从最后一项到第一项进行累计运算

函数定义:

  1. public inline fun <S, T: S> List<T>.reduceRight(operation: (T, acc: S) -> S): S {
  2. val iterator = listIterator(size)
  3. if (!iterator.hasPrevious())
  4. throw UnsupportedOperationException("Empty list can't be reduced.")
  5. var accumulator: S = iterator.previous()
  6. while (iterator.hasPrevious()) {
  7. accumulator = operation(iterator.previous(), accumulator)
  8. }
  9. return accumulator
  10. }

从函数的定义accumulator = operation(iterator.previous(), accumulator), 我们可以看出,从右边累计运算的累加子是放在后面的。

代码示例:

  1. >>> val list = listOf("a","b","c")
  2. >>> list.reduceRight({total, s -> s+total})
  3. cba

如果我们位置放错了,会输出下面的结果:

  1. >>> list.reduceRight({total, s -> total+s})
  2. abc

fold(initial: R, operation: (acc: R, T) -> R): R 带初始值的reduce

函数定义:

  1. public inline fun <T, R> Iterable<T>.fold(initial: R, operation: (acc: R, T) -> R): R {
  2. var accumulator = initial
  3. for (element in this) accumulator = operation(accumulator, element)
  4. return accumulator
  5. }

从函数的定义,我们可以看出,fold函数给累加子赋了初始值initial

代码示例:

  1. >>> val list=listOf(1,2,3,4)
  2. >>> list.fold(100,{total, next -> next + total})
  3. 110

foldRightreduceRight类似,有初始值。

函数定义:

  1. public inline fun <T, R> List<T>.foldRight(initial: R, operation: (T, acc: R) -> R): R {
  2. var accumulator = initial
  3. if (!isEmpty()) {
  4. val iterator = listIterator(size)
  5. while (iterator.hasPrevious()) {
  6. accumulator = operation(iterator.previous(), accumulator)
  7. }
  8. }
  9. return accumulator
  10. }

代码示例:

  1. >>> val list = listOf("a","b","c")
  2. >>> list.foldRight("xyz",{s, pre -> pre + s})
  3. xyzcba

forEach(action: (T) -> Unit): Unit 循环遍历元素,元素是it

我们在前文已经讲述,参看5.3.4。

再写个代码示例:

  1. >>> val list = listOf(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
  2. >>> list.forEach { value -> if (value > 7) println(value) }
  3. 8
  4. 9

forEachIndexed 带index(下标) 的元素遍历

函数定义:

  1. public inline fun <T> Iterable<T>.forEachIndexed(action: (index: Int, T) -> Unit): Unit {
  2. var index = 0
  3. for (item in this) action(index++, item)
  4. }

代码示例:

  1. >>> val list = listOf(0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
  2. >>> list.forEachIndexed { index, value -> if (value > 8) println("value of index $index is $value, greater than 8") }
  3. value of index 9 is 9, greater than 8

max 、min查询最大、最小的元素,空集则返回null

max函数定义:

  1. public fun <T : Comparable<T>> Iterable<T>.max(): T? {
  2. val iterator = iterator()
  3. if (!iterator.hasNext()) return null
  4. var max = iterator.next()
  5. while (iterator.hasNext()) {
  6. val e = iterator.next()
  7. if (max < e) max = e
  8. }
  9. return max
  10. }

返回集合中最大的元素。

代码示例:

  1. >>> val list = listOf(1,2,3)
  2. >>> list.max()
  3. 3
  4. >>> val list = listOf("a","b","c")
  5. >>> list.max()
  6. c

min函数定义:

  1. public fun <T : Comparable<T>> Iterable<T>.min(): T? {
  2. val iterator = iterator()
  3. if (!iterator.hasNext()) return null
  4. var min = iterator.next()
  5. while (iterator.hasNext()) {
  6. val e = iterator.next()
  7. if (min > e) min = e
  8. }
  9. return min
  10. }

返回集合中的最小元素。

代码示例:

  1. >>> val list = listOf(1,2,3)
  2. >>> list.min()
  3. 1
  4. >>> val list = listOf("a","b","c")
  5. >>> list.min()
  6. a

在Kotlin中,字符串的大小比较比较有意思的,我们直接通过代码示例来学习一下:

  1. >>> "c" > "a"
  2. true
  3. >>> "abd" > "abc"
  4. true
  5. >>> "abd" > "abcd"
  6. true
  7. >>> "abd" > "abcdefg"
  8. true

我们可以看出,字符串的大小比较是按照对应的下标的字符进行比较的。 另外,布尔值的比较是true大于false

  1. >>> true > false
  2. true

maxBy(selector: (T) -> R): T?minBy(selector: (T) -> R): T?获取函数映射结果的最大值、最小值对应的那个元素的值,如果没有则返回null

函数定义:

  1. public inline fun <T, R : Comparable<R>> Iterable<T>.maxBy(selector: (T) -> R): T? {
  2. val iterator = iterator()
  3. if (!iterator.hasNext()) return null
  4. var maxElem = iterator.next()
  5. var maxValue = selector(maxElem)
  6. while (iterator.hasNext()) {
  7. val e = iterator.next()
  8. val v = selector(e)
  9. if (maxValue < v) {
  10. maxElem = e
  11. maxValue = v
  12. }
  13. }
  14. return maxElem
  15. }

也就是说,不直接比较集合元素的大小,而是以集合元素为入参的函数selector: (T) -> R 返回值来比较大小,最后返回此元素的值(注意,不是对应的selector函数的返回值)。有点像数学里的求函数最值问题:

给定函数 y = f(x) , 求max f(x)x的值。

代码示例:

  1. >>> val list = listOf(100,-500,300,200)
  2. >>> list.maxBy({it})
  3. 300
  4. >>> list.maxBy({it*(1-it)})
  5. 100
  6. >>> list.maxBy({it*it})
  7. -500

对应的 minBy 是获取函数映射后返回结果的最小值所对应那个元素的值,如果没有则返回null。

代码示例:

  1. >>> val list = listOf(100,-500,300,200)
  2. >>> list.minBy({it})
  3. -500
  4. >>> list.minBy({it*(1-it)})
  5. -500
  6. >>> list.minBy({it*it})
  7. 100

sumBy(selector: (T) -> Int): Int 获取函数映射值的总和

函数定义:

  1. public inline fun <T> Iterable<T>.sumBy(selector: (T) -> Int): Int {
  2. var sum: Int = 0
  3. for (element in this) {
  4. sum += selector(element)
  5. }
  6. return sum
  7. }

可以看出,这个sumBy函数算子,累加器sum初始值为0,返回值是Int。它的入参selector是一个函数类型(T) -> Int,也就是说这个selector也是返回Int类型的函数。

代码示例:

  1. >>> val list = listOf(1,2,3,4)
  2. >>> list.sumBy({it})
  3. 10
  4. >>> list.sumBy({it*it})
  5. 30

类型错误反例:

  1. >>> val list = listOf("a","b","c")
  2. >>> list.sumBy({it})
  3. error: type inference failed: inline fun <T> Iterable<T>.sumBy(selector: (T) -> Int): Int
  4. cannot be applied to
  5. receiver: List<String> arguments: ((String) -> String)
  6. list.sumBy({it})
  7. ^
  8. error: type mismatch: inferred type is (String) -> String but (String) -> Int was expected
  9. list.sumBy({it})
  10. ^