📜  Scala中的ListMap

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

Scala中的ListMap

不可变映射使用基于列表的数据结构实现。 Scala List 类包含一个有序的线性项目列表。我们必须为 ListMap 导入 scala.collection.mutable.ListMap。 ListMap 集合仅用于少数元素。
句法:

var listMapName = ListMap("k1"->"v1", "k2"->"v2", "k3"->"v3", ...) 

这里, k是 key, v是 value。

ListMap 执行的操作

创建一个 ListMap:
在下面的代码中,我们可以看到 ListMap 是使用值创建的。

Scala
// Scala program to create or print ListMap
import scala.collection.immutable.ListMap
 
// Creating object
object Geeks
{
 
    // Main method
    def main(args: Array[String])
    {
        // Creating ListMap with values
        var listMap = ListMap("C"->"Csharp", "S"->"Scala", "J"->"Java")
         
        // Printing ListMap
        println(listMap)
    }
}


Scala
// Scala program to Adding and Accessing Elements ListMap
import scala.collection.mutable.ListMap
 
// Creating object
object Geeks
{
 
    // Main method
    def main(args: Array[String])
    {
        // Creating ListMap
        var listMap = ListMap("C"->"Csharp", "S"->"Scala", "J"->"Java")
         
        // Iterating elements
        listMap.foreach
        {
            case (key, value) => println (key + " -> " + value)        
        }
         
        // Accessing value by using key
        println(listMap("S"))
         
        // Adding element
        var ListMap2 = listMap + ("P"->"Perl")
        ListMap2.foreach
        {
            case (key, value) => println (key + " -> " + value)
        }
    }
}


Scala
// Scala program to removing Element from ListMap
import scala.collection.mutable.ListMap
 
// Creating object
object Geeks
{
 
    // Main method
    def main(args: Array[String])
    {
        // Creating ListMap
        var listMap = ListMap("C"->"Csharp", "S"->"Scala", "J"->"Java")
         
        // Iterating elements
        listMap.foreach
        {
            case (key, value) => println (key + " -> " + value)    
        }
         
        // Removing an element
        listMap -= "C"
         
        println("After Removing")
        listMap.foreach
        {
            case (key, value) => println (key + " -> " + value)
        }
    }
}


Scala
// Scala program to Create an empty ListMap
import scala.collection.mutable.ListMap
 
// Creating object
object Geeks
{
 
    // Main method
    def main(args: Array[String])
    {
        // Creating an empty list map by calling constructor
        var ListMap1 = new ListMap()  
         
        // Creating an empty list map by using .empty method
        var ListMap2 = ListMap.empty
         
        // Printing empty ListMap
        println(emptyListMap1) 
        println(emptyListMap2) 
    }
}


输出:

Map(C -> Csharp, S -> Scala, J -> Java)


添加和访问元素:
一个 ListMap 被创建,添加元素和访问元素也被执行。

斯卡拉

// Scala program to Adding and Accessing Elements ListMap
import scala.collection.mutable.ListMap
 
// Creating object
object Geeks
{
 
    // Main method
    def main(args: Array[String])
    {
        // Creating ListMap
        var listMap = ListMap("C"->"Csharp", "S"->"Scala", "J"->"Java")
         
        // Iterating elements
        listMap.foreach
        {
            case (key, value) => println (key + " -> " + value)        
        }
         
        // Accessing value by using key
        println(listMap("S"))
         
        // Adding element
        var ListMap2 = listMap + ("P"->"Perl")
        ListMap2.foreach
        {
            case (key, value) => println (key + " -> " + value)
        }
    }
}

输出:

J -> Java
C -> Csharp
S -> Scala
Scala
P -> Perl
C -> Csharp
J -> Java
S -> Scala


从 ListMap 中删除一个元素:
创建一个 ListMap,而不是使用 - 符号执行删除元素。下面是从 ListMap 中删除元素的示例。

斯卡拉

// Scala program to removing Element from ListMap
import scala.collection.mutable.ListMap
 
// Creating object
object Geeks
{
 
    // Main method
    def main(args: Array[String])
    {
        // Creating ListMap
        var listMap = ListMap("C"->"Csharp", "S"->"Scala", "J"->"Java")
         
        // Iterating elements
        listMap.foreach
        {
            case (key, value) => println (key + " -> " + value)    
        }
         
        // Removing an element
        listMap -= "C"
         
        println("After Removing")
        listMap.foreach
        {
            case (key, value) => println (key + " -> " + value)
        }
    }
}

输出:

J -> Java
C -> Csharp
S -> Scala
After Removing
J -> Java
S -> Scala


创建一个空的 ListMap:
通过调用其构造函数或使用 ListMap.empty 方法创建一个空的 ListMap。

斯卡拉

// Scala program to Create an empty ListMap
import scala.collection.mutable.ListMap
 
// Creating object
object Geeks
{
 
    // Main method
    def main(args: Array[String])
    {
        // Creating an empty list map by calling constructor
        var ListMap1 = new ListMap()  
         
        // Creating an empty list map by using .empty method
        var ListMap2 = ListMap.empty
         
        // Printing empty ListMap
        println(emptyListMap1) 
        println(emptyListMap2) 
    }
}

输出:

Map()
Map()