Kotlin基礎——Map集合詳解
Kotlin的Map集合用於保存key-value對,其也被分爲可變的和不可變的。
一、聲明和創建Map集合
Kotlin提供瞭如下函數來創建Map集合:
- mapOf():該函數返回不可變的Map集合。該函數可接受0個或多個key-value對,這些key-value對將作爲Map的元素。
- mutableMapOf():該函數返回可變的MutableMap集合。該函數可接受0個或多個key-value對,這些key-value對將作爲Map的元素。
- hashMapOf():該函數返回可變的HashMap集合。該函數可接受0個或多個key-value對,這些key-value對將作爲Map的元素。
- linkedMapOf():該函數返回可變的LinkedHashMap集合。該函數可接受0個或多個key-value對,這些key-value對將作爲Map的元素。
- sortedMapOf():該函數返回可變的TreeMap集合。該函數可接受0個或多個key-value對,這些key-value對將作爲Map的元素。
//創建不可變集合,返回值是Map
var map = mapOf("Java" to 86, "Kotlin" to 92, "Go" to 78)
println(map)//按添加順序排列
println("mapOf的返回對象的實際類型:${map.javaClass}")
//創建可變集合
var mutableMap = mutableMapOf("Java" to 86, "Kotlin" to 92, "Go" to 78)
println(mutableMap)//按添加順序排列
println("mutableMapOf的返回對象的實際類型:${mutableMap.javaClass}")
//創建HashMap集合
var hashMap = hashMapOf("Java" to 86, "Kotlin" to 92, "Go" to 78)
println(hashMap)//不保證排列順序
println("hashMapOf的返回對象的實際類型:${hashMap.javaClass}")
//創建LinkedHashMap
var linkedHashMap = linkedMapOf("Java" to 86, "Kotlin" to 92, "Go" to 78)
println(linkedHashMap)//按添加順序排列
println("linkedMapOf的返回對象的實際類型:${linkedHashMap.javaClass}")
//創建TreeMap集合
var treeMap = sortedMapOf("Java" to 86, "Kotlin" to 92, "Go" to 78)
println(treeMap)//按key由小到大排列
println("sortedMapOf的返回對象的實際類型:${treeMap.javaClass}")
輸出結果:
{Java=86, Kotlin=92, Go=78}
mapOf的返回對象的實際類型:class java.util.LinkedHashMap
{Java=86, Kotlin=92, Go=78}
mutableMapOf的返回對象的實際類型:class java.util.LinkedHashMap
{Go=78, Java=86, Kotlin=92}
hashMapOf的返回對象的實際類型:class java.util.HashMap
{Java=86, Kotlin=92, Go=78}
linkedMapOf的返回對象的實際類型:class java.util.LinkedHashMap
{Go=78, Java=86, Kotlin=92}
sortedMapOf的返回對象的實際類型:class java.util.TreeMap
二、使用Map的方法
//創建不可變集合,返回值是Map
var map = mapOf("Java" to 86, "Kotlin" to 92, "Go" to 78)
//判斷是否所有key-value對的key的長度都大於4,value都大於80
println(map.all { it.key.length > 4 && it.value > 80 })
//判斷是否任一key-value對的key的長豆都大於4、value都大於80
println(map.any { it.key.length > 4 && it.value > 80 })
println("Java" in map)
println("Go" !in map)
//對Map集合元素進行過濾:要求key包含li
val filteredMap = map.filter { "li" in it.key }
println(filteredMap)
//將每個key-value對映射成新值,返回所有新值組成的Map集合
val mappedList = map.map { "${it.key}有${it.value}節課" }
println(mappedList)
//根據key獲取最大值
println(map.maxBy { it.key })
//根據value獲取最小值
println(map.minBy { it.value })
var bMap= mapOf("Lua" to 67,"Erlang" to 73,"Kotlin" to 92)
//求並集
println(map+bMap)
//集合相減
println(map-bMap)
輸出結果:
false
true
true
false
{Kotlin=92}
[Java有86節課, Kotlin有92節課, Go有78節課]
Kotlin=92
Go=78
{Java=86, Kotlin=92, Go=78, Lua=67, Erlang=73}
{Java=86, Kotlin=92, Go=78}
三、遍歷Map四種方式
Map集合由多個key-value對組成,因此遍歷Map集合時既可以通過對key-value對進行遍歷,也可先遍歷key,再通過key來獲取對應的value進行遍歷。
- 下面是對Map集合遍歷的幾種方式:
//創建不可變集合,返回值是Map
var map = mapOf("Java" to 86, "Kotlin" to 92, "Go" to 76)
//遍歷Map的key-value對,entris元素返回key-value對組成的Set
for (en in map.entries) {
println("${en.key}->${en.value}")
}
//先遍歷Map的key,再通過key獲取value
for (key in map.keys) {
println("${key}->${map[key]}")
}
//直接用for-in循環遍歷Map
for ((key, value) in map) {
println("${key}->${value}")
}
//用Lambda表達式遍歷Map
map.forEach({ println("${it.key}->${it.value}") })
輸出結果:
Java->86
Kotlin->92
Go->76
Java->86
Kotlin->92
Go->76
Java->86
Kotlin->92
Go->76
Java->86
Kotlin->92
Go->76
四、可變的Map
可變的Map爲操作key-value對提供瞭如下方法:
- clear():清空所有的key-value對。
- put(key:K,value:V):放入key-value對。如果原來已有key,value將被覆蓋。
- putAll(form:Map<out K,V>):批量放入多個key-value對。
- remove(key:K):刪除key-value對。
var mutableMap = mutableMapOf("OC" to 96, "PHP" to 3400, "Perl" to 4300, "Ruby" to 5600, "Go" to 5600)
//以方括號語法放入key-value對
mutableMap["Swift"] = 9000
println(mutableMap)
//以put方法放入key-value對
mutableMap.put("OC", 8600)
println(mutableMap)
//刪除key爲"PHP"的key-value對
mutableMap.remove("PHP")
println(mutableMap)
println(mutableMap.size)
//刪除所有元素
mutableMap.clear()
println(mutableMap)
println(mutableMap.size)
輸出結果:
{OC=96, PHP=3400, Perl=4300, Ruby=5600, Go=5600, Swift=9000}
{OC=8600, PHP=3400, Perl=4300, Ruby=5600, Go=5600, Swift=9000}
{OC=8600, Perl=4300, Ruby=5600, Go=5600, Swift=9000}
5
{}
0
更多精彩內容請掃碼關注: