当前位置:网站首页>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)
}
边栏推荐
- LeetCode简单题之找到一个数字的 K 美丽值
- 解读创客思维与数学课程的实际运用
- buureservewp(2)
- ROS Bridge 笔记(05)— carla_ackermann_control 功能包(将Ackermann messages 转化为 CarlaEgoVehicleControl 消息)
- JS copy picture to clipboard read clipboard
- Blob object introduction
- 船载雷达天线滑环的使用
- 使用 Nocalhost 开发 Rainbond 上的微服务应用
- 快解析内网穿透为文档加密行业保驾护航
- CTF-WEB shrine模板注入nmap的基本使用
猜你喜欢
Qinglong panel - today's headlines
CCTV is so warm-hearted that it teaches you to write HR's favorite resume hand in hand
[quick start of Digital IC Verification] 11. Introduction to Verilog testbench (VTB)
What is the function of paralleling a capacitor on the feedback resistance of the operational amplifier circuit
运放电路的反馈电阻上并联一个电容是什么作用
Uniapp mobile terminal forced update function
在 Rainbond 中一键安装高可用 Nacos 集群
[quick start of Digital IC Verification] 10. Verilog RTL design must know FIFO
漏洞复现-Fastjson 反序列化
解读创客思维与数学课程的实际运用
随机推荐
积分商城管理系统中应包含的四大项
提高企业产品交付效率系列(1)—— 企业应用一键安装和升级
Four items that should be included in the management system of integral mall
Find the mode in the binary search tree (use medium order traversal as an ordered array)
JS cross browser parsing XML application
【数字IC验证快速入门】13、SystemVerilog interface 和 program 学习
Interactive book delivery - signed version of Oracle DBA work notes
青龙面板-今日头条
LeetCode中等题之我的日程安排表 I
Zcmu--1396: queue problem (2)
贝叶斯定律
DNS server configuration
太真实了,原来自己一直没有富裕起来是有原因的
Hisense TV starts the developer mode
Quick analysis of Intranet penetration helps the foreign trade management industry cope with a variety of challenges
Lua 编程学习笔记
Register of assembly language by Wang Shuang
buureservewp(2)
【数字IC验证快速入门】12、SystemVerilog TestBench(SVTB)入门
Merging binary trees by recursion