网站首页 > 技术文章 正文
forEach
高阶函数 forEach 是可迭代对象的扩展方法,接收函数类型是 (T) -> Unit 的参数 action,forEach 会将 action 这个函数作用于可迭代对象中的每个元素,这是源码:
/**
* Performs the given [action] on each element.
*/
@kotlin.internal.HidesMembers
public inline fun <T> Iterable<T>.forEach(action: (T) -> Unit): Unit {
for (element in this) action(element)
}
根据 forEach 的入参要求,我们给其传递一个 lambda 表达式或是函数引用:
fun main(args: Array<String>) {
val list = listOf(1, 2, 3, 4, 5, 6, 7, 8)
// list.forEach { it -> println(it) } // it可以省略
list.forEach { println(it) }
list.forEach(::println)
list.forEachIndexed { index, i -> println("index=$index, value=$i") }
}
forEachIndexed 相比 forEach 只是多了索引 index。
map
高阶函数 map 也是可迭代对象的扩展方法,根据 map 的源码与注释,我们知道 map 接收一个类型是 (T) -> R 的参数 transform,map 会将 transform 作用于可迭代对象中的每个元素,并最终返回一个新的集合 List:
/**
* Returns a list containing the results of applying the given [transform] function
* to each element in the original collection.
*
* @sample samples.collections.Collections.Transformations.map
*/
public inline fun <T, R> Iterable<T>.map(transform: (T) -> R): List<R> {
return mapTo(ArrayList<R>(collectionSizeOrDefault(10)), transform)
}
借助 map 的功能,我们可以将一个数组 “映射” 成另一个数组,这在日常开发很有用:
fun main(args: Array<String>) {
val list = listOf(1, 2, 3, 4, 5, 6, 7, 8)
// 将int数组中的元素经过某种运算形成一个新的int数组
val newList = list.map { it * 2 + 3 }
newList.forEach(::println)
// 将int转成double
val newList2 = list.map(Int::toDouble) // (Int) -> Double
newList2.forEach(::println)
}
注意:toDouble()是 Int 类中的一个方法,在函数引用部分已经讲过,当使用 类名::方法名 这种方式引用一个成员方法时,会自动在函数类型的参数列表第 1 位多出一个接收者 Receiver,用于接收类实例对象 ,刚好 toDouble()没有参数列表,因此 Int::toDouble 对应的函数类型是 (Int) -> Double,符合高阶函数 map 的参数要求。
flatMap
高阶函数 flatMap 比 map 高一个维度,可以将可迭代对象中的每个可迭代对象进行处理,最终返回一个 扁平化 的可迭代对象,注意参数 transform 的函数类型是 (T) -> Iterable<R>:
/**
* Returns a single list of all elements yielded from results of [transform] function being invoked on each element of original collection.
*
* @sample samples.collections.Collections.Transformations.flatMap
*/
public inline fun <T, R> Iterable<T>.flatMap(transform: (T) -> Iterable<R>): List<R> {
return flatMapTo(ArrayList<R>(), transform)
}
什么是 扁平化 ,直观的说,就是胖变瘦,多维变一维:
fun main(args: Array<String>) {
val list = listOf(
8..10,
1..3,
98..100
)
val flatList = list.flatMap { it } // it->it,这里同时也是 (Iterable) -> Iterable
flatList.forEach(::println) // 这时的 flatList 就相当于 [8,9,10,1,2,3,98,99,100]
}
flatMap 除了 扁平化 这个特性外,也拥有 map 的特性,可以将可迭代对象中的元素进行转换处理:
fun main(args: Array<String>) {
val list = listOf(
8..10,
1..3,
98..100
)
val flatList2 = list.flatMap { intRange ->
intRange.map { intElement -> "No. $intElement " } // 把Int转成String
}
flatList2.forEach(::print) // No. 8 No. 9 No. 10 No. 1 No. 2 No. 3 No. 98 No. 99 No. 100
}
filter
高阶函数 filter 可以将可迭代对象进行过滤,只有满足 predicate 过滤条件的元素(即 return true)才会被 "留下":
/**
* Returns a list containing only elements matching the given [predicate].
*
* @sample samples.collections.Collections.Filtering.filter
*/
public inline fun <T> Iterable<T>.filter(predicate: (T) -> Boolean): List<T> {
return filterTo(ArrayList<T>(), predicate)
}
我们可以使用 filter 过滤出数组中的奇数:
fun main(args: Array<String>) {
val list = listOf(1, 2, 3, 4, 5, 6, 7, 8)
val newList = list.filter { it % 2 == 1 }
newList.forEachIndexed { index, i -> print(if (index > 0) " $i" else i) } // 1 3 5 7
}
takeWhile
高阶函数 takeWhile 会在遇到第一个不符合条件的元素时就结束取数据,留下前面的作为新的集合返回:
/**
* Returns a list containing first elements satisfying the given [predicate].
*
* @sample samples.collections.Collections.Transformations.take
*/
public inline fun <T> Iterable<T>.takeWhile(predicate: (T) -> Boolean): List<T> {
val list = ArrayList<T>()
for (item in this) {
if (!predicate(item))
break
list.add(item)
}
return list
}
我们可以使用 takeWhile 筛选出前面满足条件的元素:
fun main(args: Array<String>) {
val list = listOf(1, 1, 2, 3, 5, 8, 13, 21)
var newList = list.takeWhile { it % 2 == 1 } // 筛选出前面的奇数
newList.forEachIndexed { index, i -> print(if (index > 0) " $i" else i) } // 1 1
newList = list.takeWhile { it < 5 } // 筛选出前面小于5的数
newList.forEachIndexed { index, i -> print(if (index > 0) " $i" else i) } // 1 1 2 3
}
reduce
高阶函数 reduce 会从第一个元素开始累加,并从左到右将 operation 函数应用于当前累加值和每个元素:
/**
* Accumulates value starting with the first element and applying [operation] from left to right to current accumulator value and each element.
*
* @sample samples.collections.Collections.Aggregates.reduce
*/
public inline fun <S, T : S> Iterable<T>.reduce(operation: (acc: S, T) -> S): S {
val iterator = this.iterator()
if (!iterator.hasNext()) throw UnsupportedOperationException("Empty collection can't be reduced.")
var accumulator: S = iterator.next()
while (iterator.hasNext()) {
accumulator = operation(accumulator, iterator.next())
}
return accumulator
}
我们可以用 reduce 来处理一些累加的操作,如计算 1 到 8 之间所有的数进行求和:
fun main(args: Array<String>) {
val list = listOf(1, 2, 3, 4, 5, 6, 7, 8)
val result = list.reduce { acc, i -> acc + i }
println(result) // 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 = 36
}
fold
高阶函数 fold 跟 reduce 差不多,只是 fold 多了一个初始值,后续处理与 reduce 一样:
/**
* Accumulates value starting with [initial] value and applying [operation] from left to right to current accumulator value and each element.
*/
public inline fun <T, R> Iterable<T>.fold(initial: R, operation: (acc: R, T) -> R): R {
var accumulator = initial
for (element in this) accumulator = operation(accumulator, element)
return accumulator
}
我们可以 fold 来计算在 100 的基础上,再累加 1 到 8 的和:
fun main(args: Array<String>) {
val list = listOf(1, 2, 3, 4, 5, 6, 7, 8)
val result = list.fold(100, { acc, i -> acc + i })
println(result) // 100 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 = 37
}
猜你喜欢
- 2024-09-20 Kotlin学习日记-类属性和字段(Properties/Fields)
- 2024-09-20 大厂面试原来是这样的,这份面试经你值得拥有
- 2024-09-20 Reactor 3 参考指南:5、Kotlin 支持
- 2024-09-20 Android 开发之 Kotlin 初始篇(kotlin安卓开发教程)
- 2024-09-20 Kotlin成为Android的官配编程语言
- 2024-09-20 Room & Kotlin 符号的处理(kotlin inline crossinline)
- 2024-09-20 Kotlin - 数据类型(kotlin 函数类型)
- 2024-09-20 Duolingo 如何将 Android App 全部迁至 Kotlin
- 2024-09-20 Kotlin官方文档翻译,类和对象:属性、接口、可见性修饰
- 2024-09-20 怎样把kotlin代码写好(kotlin怎么样)
- 1514℃桌面软件开发新体验!用 Blazor Hybrid 打造简洁高效的视频处理工具
- 563℃Dify工具使用全场景:dify-sandbox沙盒的原理(源码篇·第2期)
- 508℃MySQL service启动脚本浅析(r12笔记第59天)
- 486℃服务器异常重启,导致mysql启动失败,问题解决过程记录
- 485℃启用MySQL查询缓存(mysql8.0查询缓存)
- 465℃「赵强老师」MySQL的闪回(赵强iso是哪个大学毕业的)
- 445℃mysql服务怎么启动和关闭?(mysql服务怎么启动和关闭)
- 442℃MySQL server PID file could not be found!失败
- 最近发表
- 标签列表
-
- c++中::是什么意思 (83)
- 标签用于 (65)
- 主键只能有一个吗 (66)
- c#console.writeline不显示 (75)
- pythoncase语句 (81)
- es6includes (73)
- windowsscripthost (67)
- apt-getinstall-y (86)
- node_modules怎么生成 (76)
- c++int转char (75)
- static函数和普通函数 (76)
- el-date-picker开始日期早于结束日期 (70)
- js判断是否是json字符串 (67)
- checkout-b (67)
- c语言min函数头文件 (68)
- asynccallback (71)
- localstorage.removeitem (74)
- vector线程安全吗 (70)
- & (66)
- java (73)
- js数组插入 (83)
- mac安装java (72)
- eacces (67)
- 查看mysql是否启动 (70)
- 无效的列索引 (74)