当前位置:网站首页>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.6 版本发布,增加多种安装方式,优化拓扑图操作体验
- LeetCode简单题之字符串中最大的 3 位相同数字
- Niu Mei's mathematical problem --- combinatorial number
- 电池、电机技术受到很大关注,反而电控技术却很少被提及?
- Introduction à l'objet blob
- Wang Zijian: is the NFT of Tencent magic core worth buying?
- 让Livelink初始Pose与动捕演员一致
- DNS server configuration
- Example of file segmentation
- 【雅思口语】安娜口语学习记录 Part3
猜你喜欢
Unityhub cracking & unity cracking
Rainbond结合NeuVector实践容器安全管理
【数字IC验证快速入门】13、SystemVerilog interface 和 program 学习
Rainbond 5.7.1 支持对接多家公有云和集群异常报警
[quick start of Digital IC Verification] 11. Introduction to Verilog testbench (VTB)
Niu Mei's mathematical problem --- combinatorial number
Excel import function of jeesite form page
轻松上手Fluentd,结合 Rainbond 插件市场,日志收集更快捷
Rainbond 5.6 版本发布,增加多种安装方式,优化拓扑图操作体验
jeeSite 表单页面的Excel 导入功能
随机推荐
Rainbond结合NeuVector实践容器安全管理
Qinglong panel -- Huahua reading
Record a stroke skin bone error of the skirt
贝叶斯定律
【数字IC验证快速入门】10、Verilog RTL设计必会的FIFO
uniapp 移动端强制更新功能
Quick analysis of Intranet penetration helps the foreign trade management industry cope with a variety of challenges
Codeforce c.strange test and acwing
力扣(LeetCode)187. 重复的DNA序列(2022.07.06)
LeetCode简单题之找到一个数字的 K 美丽值
Network learning (II) -- Introduction to socket
CTF-WEB shrine模板注入nmap的基本使用
Network learning (I) -- basic model learning
Empire CMS collection Empire template program general
JS quick start (I)
Search for an element in a binary search tree (BST)
Myabtis_ Plus
Bugku CTF daily one question chessboard with only black chess
Uniapp mobile terminal forced update function
Recursive method to verify whether a tree is a binary search tree (BST)