📅  最后修改于: 2021-01-05 07:46:18             🧑  作者: Mango
Kotlin Map是一个界面和元素的通用集合。 Map接口以键和值对的形式保存数据。映射键是唯一的,每个键仅保留一个值。键和值可以是不同的对,例如
要使用Map接口,我们需要使用其名为mapOf()或mapOf
interface Map (source)
Properties | Description |
---|---|
abstract val entries: Set |
It returns only read all key and value pair of Set Interface in current map. |
abstract val keys: Set |
It returns only read all key of Set Interface in current map. |
abstract val keys: Set |
It returns the number of key and value pair in current map. |
abstract val values: Collection |
It returns only read Collection of all valued in current map. This collection may contain duplicate values. |
Map界面中有几个功能可用。下面介绍了Map界面的一些功能。
Functions | Description |
---|---|
fun |
It returns a value of given key or throws an exception if no such key is available in the map. |
operator fun thisRef: Any?, property: KProperty<*> ): V1 |
It returns the value of the property for the given object from current read- only map. |
operator fun |
It checks is the given key contains in map. |
fun |
If map contains the specified key it returns true. |
fun |
If map maps one or more keys to specified value it returns true. |
fun key: K, defaultValue: V ): V |
It returns the value which is given by key in mapped, or returns default value if map dose not contains mapping for the given key. |
fun |
It creates an instance of Iterable interface which wraps the original map returning its entries when being iterated. |
fun |
It creates an instance of Iterable interface which wraps the original map returning its entries when being iterated. |
fun |
It creates a Sequence interface instance which wraps the current map and returning its entries when it has iterated. |
operator fun |
It returns an Iterator over the entries in the Map. |
operator fun |
It returns a map which contains all the entries of original map except the entry of mention key. |
operator fun keys: Iterable ): Map |
It returns a map which contains all the entries of original map except those entries key which are contained in the mention key collection. |
operator fun keys: Sequence ): Map |
It returns a map which contains all the entries of original map except those entries key which are contained in the given key sequence. |
operator fun pair: Pair ): Map |
It creates a new read only map by adding or replacing an entry to current map from a given key-value pair. |
operator fun pairs: Iterable ): Map |
It creates a new read only map by adding or replacing entries to current map from a given collection of key-value pairs. |
operator fun pairs: Sequence ): Map |
It creates a new read only map by adding or replacing entries to current map from a given sequence of key-value pairs. |
让我们创建一个使用mapOf
fun main(args: Array){
val myMap = mapOf(1 to "Ajay", 4 to "Vijay", 3 to "Prakash")
for(key in myMap.keys){
println(myMap[key])
}
}
输出:
Ajay
Vijay
Prakash
更具体地说,我们可以提供通用类型Map,例如myMap:Map
fun main(args: Array){
val myMap: Map = mapOf(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
如果我们不能指定Map Interface的任何键和值类型,那么它可以采用不同类型的键和值。这是因为所有类内部都使用
fun main(args: Array){
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
fun main(args: Array){
val myMap: Map = mapOf(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))
}
输出:
Element at key 1 = Ajay
Element at key 4 = Vijay
Element at key 3 = Prakash
.....myMap.getValue(4).......
Vijay
fun main(args: Array){
val myMap: Map = mapOf(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
fun main(args: Array){
val myMap: Map = mapOf(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
fun main(args: Array){
val myMap: Map = mapOf(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
fun main(args: Array){
val myMap: Map = mapOf(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
fun main(args: Array){
val myMap: Map = mapOf(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
fun main(args: Array){
val myMap: Map = mapOf(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 = ${itr.value}")
}
}
输出:
Element at key 1 = Ajay
Element at key 4 = Vijay
Element at key 3 = Prakash
.......myMap.asIterable().....
key = 1 value = Ajay
key = 4 value = Vijay
key = 3 value = Prakash
fun main(args: Array){
val myMap: Map = mapOf(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 = ${itr1.value}")
}
}
输出:
Element at key 1 = Ajay
Element at key 4 = Vijay
Element at key 3 = Prakash
......myMap.iterator()......
key = 1 value = Ajay
key = 4 value = Vijay
key = 3 value = Prakash
fun main(args: Array){
val myMap: Map = mapOf(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
fun main(args: Array){
val myMap: Map = mapOf(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