Kotlin map接口

Kotlin Map是一个界面和元素的通用集合。 Map接口以键和值对的形式保存数据。映射键是唯一的, 每个键仅保留一个值。键和值可以是不同的对, 例如< Int, Int> , < Int, String> , < Char, String> 等。此接口是不可变的, 大小固定, 其方法支持只读访问。
要使用Map接口, 我们需要使用其名为mapOf()或mapOf < k, v> ()的函数。
地图接口声明

interface Map< K, out V> (source)

map接口的属性
物产 描述
抽象val条目:Set < Entry < K, V > > 它仅返回当前映射中Set接口的所有键和值对。
抽象val键:Set < K> 它仅返回当前映射中Set Interface的所有键。
抽象val键:Set < K> 它返回当前映射中的键和值对的数量。
抽象val值:Collection < V> 它仅返回当前映射中所有值的read Collection。此集合可能包含重复的值。
map接口功能
Map界面中有几个功能可用。下面介绍了Map界面的一些功能。
功能 描述
有趣的< K, V> 映射< 键, 值> .getValue(键:K):V 它返回给定键的值, 或者如果映射中没有可用的键则抛出异常。
运算符fun < V, V1:V> Map < in String, V> .getValue(thisRef:Any ?, property:KProperty < *> ):V1 它从当前只读映射返回给定对象的属性值。
运算符fun < K, V> Map < out K, V> .contains(key:K):布尔值 它检查给定键是否包含在映射中。
fun < K> 映射< out K, *> 。containsKey(key:K):布尔值 如果map包含指定的键, 则返回true。
fun < K, V> 映射< K, V> .containsValue(value:V):布尔值 如果map将一个或多个键映射到指定值, 则返回true。
fun < K, V> 映射< out K, V> .getOrDefault(key:K, defaultValue:V):V 它返回由映射中的键给定的值, 如果映射不包含给定键的映射, 则返回默认值。
fun < K, V> 映射< out K, V> .asIterable():Iterable < Entry < K, V > > 它创建一个Iterable接口的实例, 该实例包装原始映射并在迭代时返回其条目。
fun < K, V> 映射< out K, V> .asIterable():Iterable < Entry < K, V > > 它创建一个Iterable接口的实例, 该实例包装原始映射并在迭代时返回其条目。
fun < K, V> 映射< out K, V> .asSequence():Sequence < 条目< K, V > > 它创建一个Sequence接口实例, 该实例包装当前映射并在迭代后返回其条目。
运算符fun < K, V> 映射< out K, V> .iterator():迭代器< Entry < K, V > > 它在Map中的条目上返回一个Iterator。
运算符fun Map .minus(key:K):Map 它返回一个地图, 其中包含原始地图的所有条目(提及键的条目除外)。
运算符fun < K, V> Map < out K, V> .minus(键:Iterable < K> ):Map < K, V> 它返回一个地图, 其中包含原始地图的所有条目, 但提及键集合中包含的那些条目键除外。
运算符fun < K, V> Map < out K, V> .minus(键:Sequence < K> ):Map < K, V> 它返回一个映射, 其中包含原始映射的所有条目, 但给定键序列中包含的那些条目键除外。
运算符fun < K, V> Map < out K, V> .plus(pair:Pair < K, V> ):Map < K, V> 它通过添加或替换给定键值对中的当前映射条目来创建新的只读映射。
运算符fun < K, V> Map < out K, V> .plus(对:Iterable < Pair < K, V > > ):Map < K, V> 它通过将给定键值对集合中的条目添加或替换到当前映射中来创建新的只读映射。
运算符fun < K, V> Map < out K, V> .plus(对:Sequence < Pair < K, V > > ):Map < K, V> 它通过从给定的键-值对序列中向当前映射添加或替换条目来创建新的只读映射。
Kotlinmap接口示例1
让我们创建一个使用mapOf < k, v> ()函数声明和遍历map值的示例。在此示例中, 我们创建Int的键和String类型的值。
fun main(args: Array< String> ){val myMap = mapOf< Int, String> (1 to "Ajay", 4 to "Vijay", 3 to "Prakash") for(key in myMap.keys){ println(myMap[key]) } }

输出:
Ajay Vijay Prakash

Kotlin Map Interface示例2-通用
更具体地说, 我们可以提供通用类型Map, 例如myMap:Map < k, v> = mapOf < k, v> ()。
fun main(args: Array< String> ){val myMap: Map< Int, String> = mapOf< Int, String> (1 to "Ajay", 4 to "Vijay", 3 to "Prakash") for(key in myMap.keys){ println("Element at key $key = ${myMap.get(key)}") } }

输出:
Element at key 1 = Ajay Element at key 4 = Vijay Element at key 3 = Prakash

Kotlin Map Interface示例3-非通用
如果我们不能指定Map Interface的任何键和值类型, 则可以采用不同类型的键和值。这是因为所有类内部都使用< Any, Any> 类型。例如:
fun main(args: Array< String> ){val myMap = mapOf(1 to "Ajay", 4 to "Vijay", 3 to "Prakash", "ram" to "Ram", "two" to 2) for(key in myMap.keys){ println("Element at key $key = ${myMap.get(key)}") } }

输出:
Element at key 1 = Ajay Element at key 4 = Vijay Element at key 3 = Prakash Element at key ram = Ram Element at key two = 2

Kotlinmap接口示例4-mapOf()。getValue()
fun main(args: Array< String> ){val myMap: Map< Int, String> = mapOf< Int, String> (1 to "Ajay", 4 to "Vijay", 3 to "Prakash")for(key in myMap.keys){ println("Element at key $key = ${myMap.get(key)}") } println(".....myMap.getValue(4).......") println(myMap.getValue(4)) }

【Kotlin map接口】输出:
Element at key 1 = Ajay Element at key 4 = Vijay Element at key 3 = Prakash .....myMap.getValue(4)....... Vijay

Kotlinmap接口示例5-mapOf()。contains()
fun main(args: Array< String> ){val myMap: Map< Int, String> = mapOf< Int, String> (1 to "Ajay", 4 to "Vijay", 3 to "Prakash")for(key in myMap.keys){ println("Element at key $key = ${myMap.get(key)}") }println(".....myMap.contains(3).......") println( myMap.contains(3)) }

输出:
Element at key 1 = Ajay Element at key 4 = Vijay Element at key 3 = Prakash .....myMap.contains(3)....... true

Kotlinmap接口示例6-mapOf()。containsKey()
fun main(args: Array< String> ){val myMap: Map< Int, String> = mapOf< Int, String> (1 to "Ajay", 4 to "Vijay", 3 to "Prakash")for(key in myMap.keys){ println("Element at key $key = ${myMap.get(key)}") }println("......myMap.containsKey(2)......") println(myMap.containsKey(2)) }

输出:
Element at key 1 = Ajay Element at key 4 = Vijay Element at key 3 = Prakash ......myMap.containsKey(2)...... false

Kotlinmap接口示例7-mapOf()。containsValue()
fun main(args: Array< String> ){val myMap: Map< Int, String> = mapOf< Int, String> (1 to "Ajay", 4 to "Vijay", 3 to "Prakash")for(key in myMap.keys){ println("Element at key $key = ${myMap.get(key)}") } println("......myMap.containsValue(\"Ajay\")......") println(myMap.containsValue("Ajay")) }

输出:
Element at key 1 = Ajay Element at key 4 = Vijay Element at key 3 = Prakash ......myMap.containsValue("Ajay")...... true

Kotlinmap接口示例8-mapOf()。get()
fun main(args: Array< String> ){val myMap: Map< Int, String> = mapOf< Int, String> (1 to "Ajay", 4 to "Vijay", 3 to "Prakash")for(key in myMap.keys){ println("Element at key $key = ${myMap.get(key)}") } println(".....myMap.get(1).......") println(myMap.get(1)) }

输出:
Element at key 1 = Ajay Element at key 4 = Vijay Element at key 3 = Prakash .....myMap.get(1)....... Ajay

Kotlinmap接口示例9-mapOf()。getOrDefault()
fun main(args: Array< String> ){val myMap: Map< Int, String> = mapOf< Int, String> (1 to "Ajay", 4 to "Vijay", 3 to "Prakash")for(key in myMap.keys){ println("Element at key $key = ${myMap.get(key)}") }println("......myMap.getOrDefault(3, \"Vijay\")......") println(myMap.getOrDefault(3, "Vijay")) }

输出:
Element at key 1 = Ajay Element at key 4 = Vijay Element at key 3 = Prakash ......myMap.getOrDefault(3, "Vijay")...... Prakash

Kotlinmap接口示例10-mapOf()。asIterable()
fun main(args: Array< String> ){val myMap: Map< Int, String> = mapOf< Int, String> (1 to "Ajay", 4 to "Vijay", 3 to "Prakash")for(key in myMap.keys){ println("Element at key $key = ${myMap.get(key)}") } println(".......myMap.asIterable().....") for(itr in myMap.asIterable()){ println("key = ${itr.key} value = http://www.srcmini.com/${itr.value}") } }

输出:
Element at key 1 = Ajay Element at key 4 = Vijay Element at key 3 = Prakash .......myMap.asIterable()..... key = 1 value = http://www.srcmini.com/Ajay key = 4 value = Vijay key = 3 value = Prakash

Kotlinmap接口示例11-mapOf()。iterator()
fun main(args: Array< String> ){val myMap: Map< Int, String> = mapOf< Int, String> (1 to "Ajay", 4 to "Vijay", 3 to "Prakash")for(key in myMap.keys){ println("Element at key $key = ${myMap.get(key)}") } println("......myMap.iterator()......") for(itr1 in myMap.iterator()){ println("key = ${itr1.key} value = http://www.srcmini.com/${itr1.value}") } }

输出:
Element at key 1 = Ajay Element at key 4 = Vijay Element at key 3 = Prakash ......myMap.iterator()...... key = 1 value = http://www.srcmini.com/Ajay key = 4 value = Vijay key = 3 value = Prakash

Kotlinmap接口示例12-mapOf()。minus()
fun main(args: Array< String> ){val myMap: Map< Int, String> = mapOf< Int, String> (1 to "Ajay", 4 to "Vijay", 3 to "Prakash")for(key in myMap.keys){ println("Element at key $key = ${myMap.get(key)}") } println("......myMap.minus(4)......") for(m in myMap.minus(4)){ println(myMap[m.key]) } }

输出:
Element at key 1 = Ajay Element at key 4 = Vijay Element at key 3 = Prakash ......myMap.minus(4)...... Ajay Prakash

Kotlinmap接口示例13-mapOf()。plus()
fun main(args: Array< String> ){val myMap: Map< Int, String> = mapOf< Int, String> (1 to "Ajay", 4 to "Vijay", 3 to "Prakash")for(key in myMap.keys){ println("Element at key $key = ${myMap.get(key)}") } println("......myMap.plus(Pair(5, \"Rohan\"))......") for(p in myMap.plus(Pair(5, "Rohan"))){ println("Element at key ${p.key} = ${p.value}") }}

输出:
Element at key 1 = Ajay Element at key 4 = Vijay Element at key 3 = Prakash ......myMap.plus(Pair(5, "Rohan"))...... Element at key 1 = Ajay Element at key 4 = Vijay Element at key 3 = Prakash Element at key 5 = Rohan

    推荐阅读