当前位置:网站首页>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)
}
边栏推荐
- 复杂网络建模(二)
- Battery and motor technology have received great attention, but electric control technology is rarely mentioned?
- Complete linear regression manually based on pytoch framework
- jeeSite 表单页面的Excel 导入功能
- ROS Bridge 笔记(05)— carla_ackermann_control 功能包(将Ackermann messages 转化为 CarlaEgoVehicleControl 消息)
- B. Value sequence thinking
- Quick analysis of Intranet penetration helps the foreign trade management industry cope with a variety of challenges
- Notes on PHP penetration test topics
- Avatary的LiveDriver试用体验
- WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after conne
猜你喜欢
Quick analysis of Intranet penetration helps the foreign trade management industry cope with a variety of challenges
CTF-WEB shrine模板注入nmap的基本使用
The zblog plug-in supports the plug-in pushed by Baidu Sogou 360
海信电视开启开发者模式
CCTV is so warm-hearted that it teaches you to write HR's favorite resume hand in hand
藏书馆App基于Rainbond实现云原生DevOps的实践
追风赶月莫停留,平芜尽处是春山
The largest 3 same digits in the string of leetcode simple question
Use of JMeter
opencv学习笔记五——梯度计算/边缘检测
随机推荐
基于Pytorch 框架手动完成线性回归
offer收割机:两个长字符串数字相加求和(经典面试算法题)
Blob 對象介紹
Rainbond 5.6 版本发布,增加多种安装方式,优化拓扑图操作体验
Recursive method to construct binary tree from preorder and inorder traversal sequence
机器人教育在动手实践中的真理
饥荒云服管理脚本
CDC (change data capture technology), a powerful tool for real-time database synchronization
The zblog plug-in supports the plug-in pushed by Baidu Sogou 360
Offer harvester: add and sum two long string numbers (classic interview algorithm question)
Notes on PHP penetration test topics
【数字IC验证快速入门】10、Verilog RTL设计必会的FIFO
青龙面板--花花阅读
Interview questions (CAS)
JS copy picture to clipboard read clipboard
[quick start of Digital IC Verification] 12. Introduction to SystemVerilog testbench (svtb)
Minimum absolute difference of binary search tree (use medium order traversal as an ordered array)
buureservewp(2)
Search for an element in a binary search tree (BST)
【踩坑系列】uniapp之h5 跨域的问题