📜  科特林地图

📅  最后修改于: 2021-01-05 07:46:18             🧑  作者: Mango

Kotlin地图界面

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 Map.getValue(key: K): V It returns a value of given key or throws an exception if no such key is available in the map.
operator fun Map V>.getValue(
thisRef: Any?,

property: KProperty<*>
): V1
It returns the value of the property for the given object from current read- only map.
operator fun Map.contains(key: K): Boolean It checks is the given key contains in map.
fun Map.containsKey(key: K): Boolean If map contains the specified key it returns true.
fun Map.containsValue(value: V): Boolean If map maps one or more keys to specified value it returns true.
fun Map.getOrDefault(
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 Map.asIterable(): Iterable> It creates an instance of Iterable interface which wraps the original map returning its entries when being iterated.
fun Map.asIterable(): Iterable> It creates an instance of Iterable interface which wraps the original map returning its entries when being iterated.
fun Map.asSequence(): Sequence> It creates a Sequence interface instance which wraps the current map and returning its entries when it has iterated.
operator fun Map.iterator(): Iterator> It returns an Iterator over the entries in the Map.
operator fun Map.minus(key: K): Map It returns a map which contains all the entries of original map except the entry of mention key.
operator fun Map.minus(
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 Map.minus(
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 Map.plus(
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 Map.plus(
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 Map.plus(
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.

Kotlin地图界面示例1

让我们创建一个使用mapOf ()函数声明和遍历map值的示例。在此示例中,我们创建Int的键和String类型的值。

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

Kotlin Map Interface示例2-通用

更具体地说,我们可以提供通用类型Map,例如myMap:Map = mapOf ()。

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

Kotlin Map Interface示例3-非通用

如果我们不能指定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

Kotlin地图界面示例4-mapOf()。getValue()

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

Kotlin地图界面示例5-mapOf()。contains()

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

Kotlin地图界面示例6-mapOf()。containsKey()

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

Kotlin地图界面示例7-mapOf()。containsValue()

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

Kotlin地图界面示例8-mapOf()。get()

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

Kotlin地图界面示例9-mapOf()。getOrDefault()

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

Kotlin地图界面示例10-mapOf()。asIterable()

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

Kotlin地图界面示例11-mapOf()。iterator()

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

Kotlin地图界面示例12-mapOf()。minus()

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

Kotlin地图界面示例13-mapOf()。plus()

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