当前位置:网站首页>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中等题之我的日程安排表 I
- Jmeter 的使用
- Rainbond 5.7.1 支持对接多家公有云和集群异常报警
- Network learning (II) -- Introduction to socket
- Record a stroke skin bone error of the skirt
- Find the mode in the binary search tree (use medium order traversal as an ordered array)
- Recursive method constructs binary tree from middle order and post order traversal sequence
- Interview questions (CAS)
- JS cross browser parsing XML application
猜你喜欢

在 Rainbond 中一键安装高可用 Nacos 集群

柯基数据通过Rainbond完成云原生改造,实现离线持续交付客户
![[quick start of Digital IC Verification] 10. Verilog RTL design must know FIFO](/img/56/82f4533b5bded73df222ef65101a72.png)
[quick start of Digital IC Verification] 10. Verilog RTL design must know FIFO

轻松上手Fluentd,结合 Rainbond 插件市场,日志收集更快捷

Complex network modeling (I)

Record a stroke skin bone error of the skirt

让Livelink初始Pose与动捕演员一致

Use of JMeter

Basic use of CTF web shrink template injection nmap

Call pytorch API to complete linear regression
随机推荐
Recursive method constructs binary tree from middle order and post order traversal sequence
饥荒云服管理脚本
Niu Mei's mathematical problem --- combinatorial number
Leetcode medium question my schedule I
【数字IC验证快速入门】12、SystemVerilog TestBench(SVTB)入门
电池、电机技术受到很大关注,反而电控技术却很少被提及?
在Rainbond中一键部署高可用 EMQX 集群
Rainbond结合NeuVector实践容器安全管理
运放电路的反馈电阻上并联一个电容是什么作用
【雅思口语】安娜口语学习记录 Part2
offer收割机:两个长字符串数字相加求和(经典面试算法题)
The largest 3 same digits in the string of leetcode simple question
Complex network modeling (III)
Openjudge noi 2.1 1752: chicken and rabbit in the same cage
CCTV is so warm-hearted that it teaches you to write HR's favorite resume hand in hand
一文了解如何源码编译Rainbond基础组件
Search for an element in a binary search tree (BST)
ZCMU--1492: Problem D(C语言)
快解析内网穿透助力外贸管理行业应对多种挑战
Unityhub cracking & unity cracking