Kotlin MutableMap是集合框架的接口,该集合以键和值对的形式保存对象。 MutableMap接口的值通过使用其对应的键来检索。键和值可以是不同的对,例如<Int,Int>,<Int,String>,<Char,String>等。MutableMap的每个键仅包含一个值。
要使用MutableMap接口,无涯教程需要使用其名为mutableMapOf() 或mutableMapOf<k,v>()的函数。
interface MutableMap<K, V> : Map<K, V> (source)
Properties | Description |
---|---|
abstract val entries: MutableSet<MutableEntry<K, V>> | 这将返回映射中所有键和值对的MutableSet。 |
abstract val keys: MutableSet<K> | 这将返回此映射中MutableSet的所有键。 |
abstract val values: MutableCollection<V> | 这将返回当前映射中MutableCollection的所有值。此集合可能包含重复的值。 |
让无涯教程创建一个示例,使用mutablemapOf()函数创建一个MutableMap并遍历它。在此示例中,无涯教程以不同的方式创建了MutableMap的三种不同类型(MutableMap <Int,String>,MutableMap <String,String>和MutableMap <Any,Any>)。
fun main(args: Array<String>) { val mutableMap1: MutableMap<Int, String> = mutableMapOf<Int, String>(1 to "Ashu", 4 to "Rohan", 2 to "Ajeet", 3 to "Vijay") val mutableMap2: MutableMap<String, String> = mutableMapOf<String, String>() mutableMap2.put("name", "Ashu") mutableMap2.put("city", "Delhi") mutableMap2.put("department", "Development") mutableMap2.put("Learnfk", "Playing") val mutableMap3: MutableMap<Any, Any> = mutableMapOf<Any, Any>(1 to "Ashu", "name" to "Rohsan", 2 to 200) println(".....traverse mutableMap1........") for (key in mutableMap1.keys) { println("Key = ${key}, Value = ${mutableMap1[key]}") } println("......traverse mutableMap2.......") for (key in mutableMap2.keys) { println("Key = "+key +", "+"Value = "+mutableMap2[key]) } println("......traverse mutableMap3......") for (key in mutableMap3.keys) { println("Key = ${key}, Value = ${mutableMap3[key]}") } }
输出:
.....traverse mutableMap1........ Key = 1, Value = Ashu Key = 4, Value = Rohan Key = 2, Value = Ajeet Key = 3, Value = Vijay ......traverse mutableMap2....... Key = name, Value = Ashu Key = city, Value = Delhi Key = department, Value = Development Key = Learnfk, Value = Playing ......traverse mutableMap3...... Key = 1, Value = Ashu Key = name, Value = Rohsan Key = 2, Value = 200
函数put()和putAll()用于在MutableMap中添加元素。 put()函数一次添加单个元素,而putAll()函数在MutableMap中添加集合类型元素。
fun main(args: Array<String>) { val mutableMap: MutableMap<String, String> = mutableMapOf<String, String>() mutableMap.put("name", "Ashu") mutableMap.put("city", "Delhi") val hashMap: HashMap<String,String> = hashMapOf<String,String>() hashMap.put("department", "Development") hashMap.put("Learnfk", "Playing") println("......traverse mutableMap.......") for (key in mutableMap.keys) { println("Key = "+key +", "+"Value = "+mutableMap[key]) } mutableMap.putAll(hashMap) println("......traverse mutableMap after mutableMap.putAll(hashMap).......") for (key in mutableMap.keys) { println("Key = "+key +", "+"Value = "+mutableMap[key]) } }
输出:
......traverse mutableMap....... Key = name, Value = Ashu Key = city, Value = Delhi ......traverse mutableMap after mutableMap.putAll(hashMap)....... Key = name, Value = Ashu Key = city, Value = Delhi Key = department, Value = Development Key = Learnfk, Value = Playing
containsKey()函数用于检查指定的键是否在MutableMap中存在。如果包含指定的键,则返回true,否则返回false。例如:
fun main(args: Array<String>) { val mutableMap: MutableMap<String, String> = mutableMapOf<String, String>() mutableMap.put("name", "Ashu") mutableMap.put("city", "Delhi") mutableMap.put("department", "Development") mutableMap.put("Learnfk", "Playing") println("......traverse mutableMap.......") for (key in mutableMap.keys) { println("Key = "+key +", "+"Value = "+mutableMap[key]) } println("......mutableMap.containsKey(\"city\").......") println(mutableMap.containsKey("city")) }
输出:
......traverse mutableMap....... Key = name, Value = Ashu Key = city, Value = Delhi Key = department, Value = Development Key = Learnfk, Value = Playing ......mutableMap.containsKey("city")....... true
containsValue()函数用于检查指定的值是否存在于MutableMap中。如果映射映射给定值的一个或多个键,则此函数返回true,否则返回false。例如:
fun main(args: Array<String>) { val mutableMap: MutableMap<String, String> = mutableMapOf<String, String>() mutableMap.put("name", "Ashu") mutableMap.put("city", "Delhi") mutableMap.put("department", "Development") mutableMap.put("Learnfk", "Playing") println("......traverse mutableMap.......") for (key in mutableMap.keys) { println("Key = "+key +", "+"Value = "+mutableMap[key]) } println(".......mutableMap.containsValue(\"Delhi\")......") println(mutableMap.containsValue("Delhi")) println(".......mutableMap.containsValue(\"Mumbai\")......") println(mutableMap.containsValue("Mumbai")) }
输出:
......traverse mutableMap....... Key = name, Value = Ashu Key = city, Value = Delhi Key = department, Value = Development Key = Learnfk, Value = Playing .......mutableMap.containsValue("Delhi")...... true .......mutableMap.containsValue("Mumbai")...... false
contains()函数用于检查MutableMap中是否存在指定的键值。如果MutableMap中存在指定的键或值,则它将返回true,否则将返回false。例如:
fun main(args: Array<String>) { val mutableMap: MutableMap<String, String> = mutableMapOf<String, String>() mutableMap.put("name", "Ashu") mutableMap.put("city", "Delhi") mutableMap.put("department", "Development") mutableMap.put("Learnfk", "Playing") println("......traverse mutableMap.......") for (key in mutableMap.keys) { println("Key = "+key +", "+"Value = "+mutableMap[key]) } println("......mutableMap.contains(\"city\").......") println(mutableMap.contains("city")) }
输出:
......traverse mutableMap....... Key = name, Value = Ashu Key = city, Value = Delhi Key = department, Value = Development Key = Learnfk, Value = Playing ......mutableMap.contains("city")....... true
get(key)函数用于在MutableMap中检索指定键的对应值。如果MutableMap中不存在这样的键,则它返回null。例如:
fun main(args: Array<String>) { val mutableMap: MutableMap<String, String> = mutableMapOf<String, String>() mutableMap.put("name", "Ashu") mutableMap.put("city", "Delhi") mutableMap.put("department", "Development") mutableMap.put("Learnfk", "Playing") println("......traverse mutableMap.......") for (key in mutableMap.keys) { println("Key = "+key +", "+"Value = "+mutableMap[key]) } println(".......mutableMap.get(\"department\")......") println(mutableMap.get("department")) }
输出:
......traverse mutableMap....... Key = name, Value = Ashu Key = city, Value = Delhi Key = department, Value = Development Key = Learnfk, Value = Playing .......mutableMap.get("department")...... Development
用于返回MutableMap的指定键的对应值的getValue()函数,或者如果在地图中找不到键,则抛出异常。例如:
fun main(args: Array<String>) { val mutableMap: MutableMap<String, String> = mutableMapOf<String, String>() mutableMap.put("name", "Ashu") mutableMap.put("city", "Delhi") mutableMap.put("department", "Development") mutableMap.put("Learnfk", "Playing") println("......traverse mutableMap.......") for (key in mutableMap.keys) { println("Key = ${key}, Value = ${mutableMap[key]}") } println(".......mutableMap.getValue(\"department\")......") println(mutableMap.getValue("department")) }
输出:
......traverse mutableMap....... Key = name, Value = Ashu Key = city, Value = Delhi Key = department, Value = Development Key = Learnfk, Value = Playing .......mutableMap.getValue("department")...... Development
getOrDefault()函数返回MutableMap的指定键的对应值。如果MutableMap中不存在此类键,则它将返回默认提及值。例如:
fun main(args: Array<String>) { val mutableMap: MutableMap<String, String> = mutableMapOf<String, String>() mutableMap.put("name", "Ashu") mutableMap.put("city", "Delhi") mutableMap.put("department", "Development") mutableMap.put("Learnfk", "Playing") println("......traverse mutableMap.......") for (key in mutableMap.keys) { println("Key = ${key}, Value = ${mutableMap[key]}") } println(".......mutableMap.getOrDefault(\"name\", \"Default Value\")......") println(mutableMap.getOrDefault("name", "default value")) }
输出:
......traverse mutableMap....... Key = name, Value = Ashu Key = city, Value = Delhi Key = department, Value = Development Key = Learnfk, Value = Playing .......mutableMap.getOrDefault("name", "Default Value")...... Ashu
count()函数用于返回MutableMap中存在的元素总数。例如:
fun main(args: Array<String>) { val mutableMap: MutableMap<String, String> = mutableMapOf<String, String>() mutableMap.put("name", "Ashu") mutableMap.put("city", "Delhi") mutableMap.put("department", "Development") mutableMap.put("Learnfk", "Playing") println("......traverse mutableMap.......") for (key in mutableMap.keys) { println("Key = ${key}, Value = ${mutableMap[key]}") } println(".....mutableMap.count()........") println(mutableMap.count()) }
输出:
......traverse mutableMap....... Key = name, Value = Ashu Key = city, Value = Delhi Key = department, Value = Development Key = Learnfk, Value = Playing .....mutableMap.count()........ 4
remove(key)函数用于删除与其提及键相对应的值。而remove(key,value)函数将删除包含key和value的元素。如果remove(key,value)函数将指定的键及其值一起移除,则返回true,否则返回false。例如:
fun main(args: Array<String>) { val mutableMap: MutableMap<String, String> = mutableMapOf<String, String>() mutableMap.put("name", "Ashu") mutableMap.put("city", "Delhi") mutableMap.put("department", "Development") mutableMap.put("Learnfk", "Playing") println("......traverse mutableMap.......") for (key in mutableMap.keys) { println("Key = ${key}, Value = ${mutableMap[key]}") } println("......mutableMap.remove(\"city\").......") println(mutableMap.remove("city")) println(".......mutableMap.remove(\"Learnfk\",\"Playing\")......") println(mutableMap.remove("Learnfk","Playing")) println("......traverse mutableMap.......") for (key in mutableMap.keys) { println("Key = ${key}, Value = ${mutableMap[key]}") } }
输出:
......traverse mutableMap....... Key = name, Value = Ashu Key = city, Value = Delhi Key = department, Value = Development Key = Learnfk, Value = Playing ......mutableMap.remove("city")....... Delhi .......mutableMap.remove("Learnfk","Playing")...... true ......traverse mutableMap after remove....... Key = name, Value = Ashu Key = department, Value = Development
clear()函数用于从MutableMap中删除所有元素。例如:
fun main(args: Array<String>) { val mutableMap: MutableMap<String, String> = mutableMapOf<String, String>() mutableMap.put("name", "Ashu") mutableMap.put("city", "Delhi") mutableMap.put("department", "Development") mutableMap.put("Learnfk", "Playing") println("......traverse mutableMap.......") for (key in mutableMap.keys) { println("Key = ${key}, Value = ${mutableMap[key]}") } println("......mutableMap.clear().......") println(mutableMap.clear()) println(mutableMap) }
输出:
......traverse mutableMap....... Key = name, Value = Ashu Key = city, Value = Delhi Key = department, Value = Development Key = Learnfk, Value = Playing ......mutableMap.clear()....... kotlin.Unit {}
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)