当前位置:网站首页>Kotlin learning three: set and lambda
Kotlin learning three: set and lambda
2022-07-22 20:07:00 【Wu Qingsen】
List Created in traversal
Create a read only collection , Can not add 、 Modify or delete the contents of the collection :
val myList = listOf("A", "B", "C")
for (i in 0 until myList.size) {
Log.w(" list ", "" + myList.get(i))
}
Create a set that can be modified :
val myList = mutableListOf("A", "B", "C")
myList.add("D")
for (i in 0 until myList.size) {
Log.w(" list ", "" + myList.get(i))
}
Map Use
establish , Save data and get data :
// establish
val myMap = HashMap<Int, String>()
// Add data
myMap[1] = "A"
myMap[2] = "B"
myMap[3] = "C"
// Take out the data
val value = myMap[2]
Log.w(" Take out the data ", "" + value)
You can write like this :
// establish
val myMap = mutableMapOf(1 to "A", 2 to "B", 3 to "C")
// Add data
myMap[8] = "X"
// Take out the data
for ((myK, myV) in myMap) {
Log.w(" Take out the data ", "key:" + myK + ",value:" + myV)
}
There is no mutable Is only readable , Yes mutable It can be modified .
Lambda expression
Lambda An expression is an expression with parameters .
Grammatical structure :{ Parameter name 1: Parameter type , Parameter name 2: Parameter type -> The body of the function }
It is simply understood as a method that can pass in a piece of code . Generally, I write it myself , You can only pass in parameters and then process them in the method body , and Lambda It can send a piece of code for processing .
Let's take a simple example , I want to take the longest data in a list :
val myList = listOf("aA", "BBBbb", "CCcc")
val lambda = { name: String -> name.length }
val maxLength = myList.maxBy(lambda)
Log.w(" Take out the longest ", "" + maxLength)
It can be simplified as :
val myList = listOf("aA", "BBBbb", "CCcc")
val maxLength = myList.maxBy({ name: String -> name.length })
Log.w(" Take out the longest ", "" + maxLength)
kotlin Regulations , The last parameter is zero lambda when , Can be lambda Put it outside , Then omit () , So it's like this :
val myList = listOf("aA", "BBBbb", "CCcc")
val maxLength = myList.maxBy{ name: String -> name.length }
Log.w(" Take out the longest ", "" + maxLength)
because lambda There is only one parameter , So parameters can be omitted , use it Keyword substitution , So it's like this :
val myList = listOf("aA", "BBBbb", "CCcc")
val maxLength = myList.maxBy { it.length }
Log.w(" Take out the longest ", "" + maxLength)
Another example , Want to capitalize everything , be used map function :
val myList = listOf("aA", "BBBbb", "CCcc")
val newList = myList.map { it.toUpperCase() }
for (value in newList) {
Log.w(" The value is ", value)
}
above map Function can map each element in the list to other values .
Use filter The filter length of the function is 4 What's inside , And they are all capitalized :
val myList = listOf("aA", "BBBbb", "CCcc")
val newList = myList.filter { it.length < 5 }.map { it.toUpperCase() }
for (value in newList) {
Log.w(" The value is ", value)
}
filter You can filter the data in the set .
any function : Judge whether at least one of the set satisfies the condition ;
all function : Determine whether all elements in the set meet the conditions ;
val myList = listOf("aA", "BBBbb", "CCcc")
val mAny = myList.any { it.length > 3 }
val mAll = myList.all { it.length > 3 }
Log.w(" return ", "" + mAny + "," + mAll)
边栏推荐
- Spark RDD depends on the working principle of DAG
- Installation of mysql5.7 (yum, binary, compile and install)
- Flutter 2进阶(二):Flutter空安全
- leetcode 32. 最长有效括号
- RRPN:Arbitrary-Oriented Scene Text Detection via Rotation Proposals
- Error reported by Xcode compiling pod third-party library
- Five levels of people and products
- The difference between location.replace and location.href
- Pregel function in spark graphx (Reprint)
- Datablau5.0 data asset management product series heavy release
猜你喜欢
Datablau5.0 data asset management product series heavy release
shell script “<< EOF”我的用途和遇到的问题
Opening soon | openatom openharmony sub forum of 2022 open atom global open source summit "interconnection of all things, enabling thousands of industries"
vmware nat模式下主机ping不通虚拟机:跟大部分方法不一样
PLSQL cannot be initialized
Kotlin学习一:变量、函数、条件语句与循环语句
jps没有namenode和datanode原因
Open MySQL binlog log on Linux
Spark SQL 内置函数和自定义函数UDF
【TA-霜狼_may-《百人计划》】图形3.3 曲面细分与几何着色器 大规模草渲染
随机推荐
Spark RDD算子:RDD分区,HashPartitioner、RangePartitioner、自定义分区
Fixed left width, adaptive right width
How to clear localstorage data after closing the browser
Spark RDD的持久化(缓存、检查点、广播变量和累加器)
Spark GraphX 中的 pregel函数(转载)
Spark RDD的依赖于DAG的工作原理
Xcode11 添加lanuchimage黑屏无法显示问题
Flutter 2进阶(九):FijkPlayer播放视频与卡片效果
nacos权限与数据库
Pregel function in spark graphx (Reprint)
Structure, enumeration, joint blog tutorial
oc 项目倒入swift 代码注意事项
[chat] essays after two years of work
抽象类,接口
数组push时 覆盖的问题
Flutter development (32): flutter screen adaptation
Five levels of people and products
Summary of MySQL grant user permissions
stat函数详解
try catch