当前位置:网站首页>Transformation function map and flatmap in kotlin
Transformation function map and flatmap in kotlin
2022-07-07 08:15:00 【yu-Knight】
//Kotlin Transformation function in map And flatmap
fun main() {
val list = listOf(" Zhang San ", " Li Si ", " Wang Wu ")
// principle : Add the return value of the last line of the anonymous function to a new set , The generic type of the new set is R, And return a new set
val list2: List<Int> = list.map {
//it == T == Elements == String
"[$it]"
88
}
println(list2)
// purpose : and RxJava The same way of thinking
val list3: List<String> = list.map {
" Name is :$it"
}.map {
"$it, Text length is :${it.length}"
}.map {
"[$it]"
}
for (s in list3) {
println("$s ")
}
//map{ Return type :T String Int Boolean... Add each element to the new set , Finally back to List<String>}
//flatMap{ Return type : Every element T aggregate 1 aggregate 2... Put every element ( aggregate ) Add to new collection , Finally, return the new set List<List<String>>
// Finally, the internal will be processed into List<String>
//flatMap amount to List<List<String>> A collection of collections , There are nested relationships
val list4: List<String> = listOf(" Zhang San ", " Li Si ", " Wang Wu ")
val newList: List<String> = list4.map {
" The content is :$it" // One at a time String
}.map {
"$it, The length is :${it.length}"
}.flatMap {
listOf("$it, I'm learning C++", "$it, I'm learning Java", "$it, I'm learning Kotlin")
}
println(newList)
val newList2: List<String> = list.flatMap {
listOf("$it, I'm learning C++", "$it, I'm learning Java", "$it, I'm learning Kotlin")
}
println(newList2)
}边栏推荐
- Rainbond 5.7.1 支持对接多家公有云和集群异常报警
- The largest 3 same digits in the string of leetcode simple question
- 力扣(LeetCode)187. 重复的DNA序列(2022.07.06)
- 发挥创客教育空间的广泛实用性
- Recursive method constructs binary tree from middle order and post order traversal sequence
- 【无标题】
- It's too true. There's a reason why I haven't been rich
- Pytoch (VI) -- model tuning tricks
- Topic not received? Try this
- Find the mode in the binary search tree (use medium order traversal as an ordered array)
猜你喜欢

Hisense TV starts the developer mode

Leetcode simple question: find the K beauty value of a number

【数字IC验证快速入门】13、SystemVerilog interface 和 program 学习

Niu Mei's mathematical problem --- combinatorial number
![[quick start of Digital IC Verification] 11. Introduction to Verilog testbench (VTB)](/img/c2/32a2c1ede493b778a6c44077d765d0.png)
[quick start of Digital IC Verification] 11. Introduction to Verilog testbench (VTB)

UnityHub破解&Unity破解

What is the function of paralleling a capacitor on the feedback resistance of the operational amplifier circuit

【数字IC验证快速入门】10、Verilog RTL设计必会的FIFO

eBPF Cilium实战(2) - 底层网络可观测性

LeetCode中等题之我的日程安排表 I
随机推荐
Leetcode simple question: find the K beauty value of a number
漏洞复现-Fastjson 反序列化
offer收割机:两个长字符串数字相加求和(经典面试算法题)
Qinglong panel -- Huahua reading
eBPF Cilium实战(1) - 基于团队的网络隔离
Myabtis_ Plus
轻松上手Fluentd,结合 Rainbond 插件市场,日志收集更快捷
Four items that should be included in the management system of integral mall
调用 pytorch API完成线性回归
在Rainbond中一键部署高可用 EMQX 集群
Empire CMS collection Empire template program general
柯基数据通过Rainbond完成云原生改造,实现离线持续交付客户
Don't stop chasing the wind and the moon. Spring mountain is at the end of Pingwu
Blob object introduction
在Rainbond中实现数据库结构自动化升级
复杂网络建模(三)
Game attack and defense world reverse
Content of string
LeetCode简单题之判断一个数的数字计数是否等于数位的值
一文了解如何源码编译Rainbond基础组件