📜  Kotlin 地图:mapOf()

📅  最后修改于: 2022-05-13 01:54:20.762000             🧑  作者: Mango

Kotlin 地图:mapOf()

Kotlin map 是一个包含对象对的集合。 Map 以由键和值组成的对形式保存数据。映射键是唯一的,并且映射只为每个键保存一个值。
Kotlin 区分不可变映射和可变映射。使用 mapOf( )创建的不可变映射意味着这些是只读的,而使用mutableMapOf()创建的可变映射意味着我们可以同时执行读取和写入。
句法 :

fun  mapOf(vararg pairs: Pair): Map 
  • 该对的第一个值是,第二个是对应键的
  • 如果多个对具有相同的键,则 map 将返回最后一个对值。
  • 映射条目按指定顺序遍历。

mapOf() 的 Kotlin 程序

Java
fun main(args: Array)
{
    //declaring a map of integer to string
    val map = mapOf(1 to "Geeks", 2 to "for" , 3 to "Geeks")
    //printing the map
    println( map)
}


Java
fun main(args: Array)
{
    //declaring a map of integer to string
    val map = mapOf(1 to "One", 2 to "Two" , 3 to "Three", 4 to "Four")
 
    println("Map Entries : "+map)
 
    println("Map Keys: "+map.keys )
 
    println("Map Values: "+map.values )
}


Java
fun main() {
 
    val ranks = mapOf(1 to "India",2 to "Australia",3 to "England",4 to "Africa")
    //method 1
    println("The size of the map is: "+ranks.size)
    //method 2
    println("The size of the map is: "+ranks.count())
}


Java
fun main(args: Array)
{
    //here we have created an empty map using mapOf()
    val map1 = mapOf()
      
    println("Entries: " + map1.entries)  //entries of map
  
    println("Keys:" + map1.keys)  //keys of map
  
    println("Values:" + map1.values)  //values of map
  
}


Java
fun main() {
 
    val ranks = mapOf(1 to "India",2 to "Australia",3 to "England",4 to "Africa")
 
    //method 1
    println("Team having rank #1 is: "+ranks[1])
    //method 2
    println("Team having rank #3 is: "+ranks.getValue(3))
    //method 3
    println("Team having rank #4 is: "+ranks.getOrDefault(4, 0))
    // method  4
    val team = ranks.getOrElse(2 ,{ 0 })
    println(team)
}


Java
fun main() {
    val colorsTopToBottom = mapOf("red" to 1, "orange" to 2, "yellow" to 3,
        "green" to 4 , "blue" to 5, "indigo" to 6, "violet" to 7)
    var color = "yellow"
    if (colorsTopToBottom.containsKey(color)) {
        println("Yes, it contains color $color")
    } else {
        println("No, it does not contain color $color")
    }
    val value = 8
    if (colorsTopToBottom.containsValue(value)) {
        println("Yes, it contains value $value")
    } else {
        println("No, it does not contain value $value")
    }
}


Java
fun main(args: Array)
{
    //lets make two values with same key
    val map = mapOf(1 to "geeks1",2 to "for" , 1 to "geeks2")
    // return the map entries
    println("Entries of map : " + map.entries)
}


输出:

{1=Geeks, 2=for, 3=Geeks}

映射键、值和条目 –

Java

fun main(args: Array)
{
    //declaring a map of integer to string
    val map = mapOf(1 to "One", 2 to "Two" , 3 to "Three", 4 to "Four")
 
    println("Map Entries : "+map)
 
    println("Map Keys: "+map.keys )
 
    println("Map Values: "+map.values )
}

输出:

Map Entries : {1=One, 2=Two, 3=Three, 4=Four}
Map Keys: [1, 2, 3, 4]
Map Values: [One, Two, Three, Four] 

地图大小 –

我们可以使用两种方法来确定地图的大小。通过使用地图的size属性和使用count()方法。

Java

fun main() {
 
    val ranks = mapOf(1 to "India",2 to "Australia",3 to "England",4 to "Africa")
    //method 1
    println("The size of the map is: "+ranks.size)
    //method 2
    println("The size of the map is: "+ranks.count())
}

输出:

The size of the map is: 4
The size of the map is: 4 

空地图——

我们可以使用mapOf()创建一个空的可序列化映射
mapOf() 示例 2

Java

fun main(args: Array)
{
    //here we have created an empty map using mapOf()
    val map1 = mapOf()
      
    println("Entries: " + map1.entries)  //entries of map
  
    println("Keys:" + map1.keys)  //keys of map
  
    println("Values:" + map1.values)  //values of map
  
}

输出:

Entries: []
Keys:[]
Values:[] 

获取 Map 的值 –

我们可以使用下面程序中讨论的不同方法从地图中检索值。

Java

fun main() {
 
    val ranks = mapOf(1 to "India",2 to "Australia",3 to "England",4 to "Africa")
 
    //method 1
    println("Team having rank #1 is: "+ranks[1])
    //method 2
    println("Team having rank #3 is: "+ranks.getValue(3))
    //method 3
    println("Team having rank #4 is: "+ranks.getOrDefault(4, 0))
    // method  4
    val team = ranks.getOrElse(2 ,{ 0 })
    println(team)
}

输出:

Team having rank #1 is: India
Team having rank #3 is: England
Team having rank #4 is: Africa
Australia

地图包含键或值 -

我们可以分别使用 containsKey() 和 containsValue() 方法来确定映射是否包含键或值。

Java

fun main() {
    val colorsTopToBottom = mapOf("red" to 1, "orange" to 2, "yellow" to 3,
        "green" to 4 , "blue" to 5, "indigo" to 6, "violet" to 7)
    var color = "yellow"
    if (colorsTopToBottom.containsKey(color)) {
        println("Yes, it contains color $color")
    } else {
        println("No, it does not contain color $color")
    }
    val value = 8
    if (colorsTopToBottom.containsValue(value)) {
        println("Yes, it contains value $value")
    } else {
        println("No, it does not contain value $value")
    }
}

输出:

Yes, it contains color yellow
No, it does not contain value 8 

两个值和相同的键 -

如果两个具有相同的键值,则映射将包含这些数字的最后一个值
mapOf() 示例 3

Java

fun main(args: Array)
{
    //lets make two values with same key
    val map = mapOf(1 to "geeks1",2 to "for" , 1 to "geeks2")
    // return the map entries
    println("Entries of map : " + map.entries)
}

输出:

Entries of map : [1=geeks2, 2=for]

解释 :
这里的键值1已经用两个值初始化: geeks1 和 geeks2 ,但是我们知道 mapOf ()对一个键项只能有一个值,因此最后一个值仅由 map 存储,并且geeks1被消除。