当前位置:网站首页>Use of kotlin basic common sets list, set and map
Use of kotlin basic common sets list, set and map
2022-06-13 06:24:00 【m0_ forty-seven million nine hundred and fourteen thousand one 】
Set is a kind of data structure we often use , Can be passed as a value to a function , Like other variable types we've learned ,List、Set、Map There are two types of , Read only and variable .
One .List aggregate
1.List Create and get elements
getOrElse Is a security index value function , He needs two parameters , The first is the index value , The second is to provide default values lambda expression , If the index value does not exist , Can be used instead of exception .
getOrNull yes Kotlin Another security index value function provided by , It returns null result , Instead of throwing an exception .
fun main() {
//listOf Create immutable collections
val listOf = listOf("Jason", "Jack", "Jacky")
// Index value output exceeded lambda Expression content
println(listOf.getOrElse(3){"Unknown"})
// Index value output exceeded null
println(listOf.getOrNull(3))
}
2. variable List aggregate
stay Kotlin in , The list that supports content modification is called variable list , To create a variable list , have access to mutableListOf function .List You can also use toList and toMutableList Function to dynamically realize the exchange of self reading list and variable list .
fun main() {
//mutableListOf Create variable sets
val mutableList = mutableListOf("Jason", "Jack", "Jacky")
// Add an element
mutableList.add("XiaoHua")
// Remove an element
mutableList.remove("XiaoHua")
// Turn immutable
val toList = mutableList.toList()
// Turn variable
val toMutableList = toList.toMutableList()
println(mutableList)
println(toList)
println(toMutableList)
}3.mutator function
Functions that can modify variable lists have a unified name :mutator function
fun main() {
//mutableListOf Create variable sets
val mutableList = mutableListOf("Jason", "Jack", "Jacky")
//+= And add The same effect
mutableList+="Jimmy"
//-= And remove The same effect
mutableList-="Jimmy"
// Remove the qualified parameters
mutableList.removeIf {
it.contains("Jack")
}
println(mutableList)
}4. A collection of traverse
fun main() {
//mutableListOf Create a read-only collection
val list=listOf("Jason", "Jack", "Jacky")
// Traverse
for (s in list) {
print(s)
}
// Traverse
list.forEach {
print(it)
}
// Traversal is to get the index
list.forEachIndexed { index, item ->
print("$index, $item")
}
}
5. Deconstruction and de duplication
fun main() {
//mutableListOf Create a read-only collection
val list=listOf("Jason", "Jack", "Jacky")
// Structural grammar
// hold list Values in the set Assign to the previous attribute You can skip the attribute assignment of this position by underlining it
val (origin,_,roxy)=list
print("$origin $roxy")
print(listOf("Jason", "Jack", "Jacky", "Jack").distinct()) //list There are also de duplication methods
}Two .Set aggregate
1.Set Create and get elements
adopt setOf establish Set aggregate , Use elementAt Function to read the elements in the collection .
fun main() {
// immutable set aggregate
//set Set the automatic de duplication
val set = setOf("Jason", "Jack", "Jacky", "Jack")
//set[1] There is no such way of writing
println(set.elementAt(2)) // Take the element
}2. Variable set
adopt mutableSetOf Create variable set aggregate
fun main() {
// variable set aggregate
//set Set the automatic de duplication
val mutableSetOf = mutableSetOf("Jason", "Jack", "Jacky", "Jack")
// Additive elements
mutableSetOf+="XiaoHua"
//Set Set value
print(mutableSetOf.elementAt(2))
}3. Set transformation
fun main() {
val mutableList = mutableListOf("Jason", "Jack", "Jacky", "Jack")
// We can go through List Collective transfer Set Collective insect repellent
val toList = mutableList.toSet()
// Turn it back List aggregate
val toList1 = toList.toMutableList()
println(toList1)
}3、 ... and .Map aggregate
1.Map establish
to Although it looks like a keyword , But it is actually a special function that omits the period and parameters ,to The function converts its left and right values into a pair of Pair
fun main() {
val mapOf2 = mapOf("123" to 123, "Jason" to 18, "Jack" to 30)// It's also a way
//123 to 123 It's actually "!23".to(123) Because in the infix keyword . and () All omitted
val mapOf1 = mapOf(Pair("312", 12312), Pair("123", 124214))// This is also a way to create
println(mapOf1)
println(mapOf2)
}
2. Read Map Value
fun main() {
val mapOf2 = mapOf("123" to 123, "Jason" to 18, "Jack" to 30)// It's also a way
// These are Map Set value method
print( mapOf2.get("Jack"))
print( mapOf2.getValue("Jack")) // If the value to be taken is null , Abnormal Report
print( mapOf2.getOrElse("13"){"Unknown"}) // There is no such value Put it back lambda The content of the expression
print( mapOf2.getOrDefault("13",0)) // There is no such value , Enter the default value
print(mapOf2["123"])
}3. Traverse
fun main() {
val mapOf2 = mapOf("123" to 123, "Jason" to 18, "Jack" to 30)// It's also a way
// The following is the traversal method
mapOf2.forEach {
print("${it.key} ${it.value} ")
}
// Parameters deconstruct
mapOf2.forEach { (key, value) ->
print("$key $value" )
}
}
4. Variable set
adopt mutableMapOf Create variable Map.
fun main() {
val mutableMapOf = mutableMapOf("123" to 123, "Jason" to 18, "Jack" to 30)
mutableMapOf +="12312" to 42124 // Additive elements
mutableMapOf["1231241"] = 12213 // Modifying elements
mutableMapOf.getOrPut("Rose",{12312}) // Take value first , When there is no current value to get in the set , A default value will be filled in
print(mutableMapOf)
Next : Kotlin Basics Defining classes and initializing 、 Inherit
边栏推荐
- Regular verification of mobile phone number, landline email ID card
- js将文本转成语言播放
- Echart histogram: stack histogram value formatted display
- 动态链接库嵌套样例
- Analysis of 43 cases of MATLAB neural network: Chapter 11 optimization of continuous Hopfield Neural Network -- optimization calculation of traveling salesman problem
- Dart class inherits and implements mixed operators
- Hbuilderx: installation of hbuilderx and its common plug-ins
- Wechat applet custom tabbar (session customer service) vant
- ADB shell content command debug database
- JS to realize bidirectional data binding
猜你喜欢

The web server failed to start Port 7001 was already in use
![[2022 college entrance examination season] what I want to say as a passer-by](/img/d7/3813b944dc2df182455d475a3669fb.jpg)
[2022 college entrance examination season] what I want to say as a passer-by

Intelligent digital asset management helps enterprises win the post epidemic Era
![[solution] camunda deployment process should point to a running platform rest API](/img/ef/5b893e9c315c10db6c1db46b4c3f5a.jpg)
[solution] camunda deployment process should point to a running platform rest API

js将文本转成语言播放

You should consider upgrading via

无刷直流电机矢量控制(四):基于滑模观测器的无传感器控制

Echart line chart: multiple line charts show only one line at a time

Recommend a capacity expansion tool to completely solve the problem of insufficient disk space in Disk C and other disks

MFS详解(六)——MFS Chunk Server服务器安装与配置
随机推荐
You should consider upgrading via
ADB shell CMD overlay debugging command facilitates viewing system framework character resource values
【js】var、let、const
线程相关点
Echart line chart: different colors are displayed when the names of multiple line charts are the same
Echart柱状图:堆叠柱状图value格式化显示
Uniapp (upload local pictures, preview pictures, convert Base64 format, upload audio files)
SSM framework integration -- > simple background management
【新手上路常见问答】关于技术管理
Echart折线图:多条折线图每次仅展示一条
Recommend a capacity expansion tool to completely solve the problem of insufficient disk space in Disk C and other disks
万能播放器 PotPlayer 的下载与安装,直播流 m3u8 导入
[written examination questions of meituan]
HBuilderX:HBuilderX安装以及其常用插件安装
USB status error and its cause (error code)
Echart histogram: echart implements stacked histogram
本地文件秒搜工具 Everything
Uniapp dynamically shows / hides the navigation bar return button
Multiple reception occurs in the uniapp message delivery
347. top k high frequency elements heap sort + bucket sort +map