Scala地图

Scala地图 (Scala maps)

A map is a collection that stores its elements as key-value pairs, like a dictionary. Also, known as hash tables, maps have unique keys that are used to retrieve the value related to the key.

映射是将其元素存储为键-值对的集合,例如字典。 同样,称为哈希表的映射具有唯一的键,这些键用于检索与键有关的值。

Maps can be of two kinds:

地图可以有两种:

Mutable and immutable, for immutable variables the objects can't be changed in the program after initialization. By default the maps are immutable, to use the mutable maps you have to use scala.collection.mutable.Map class.

可变和不可变 ,对于不可变变量,初始化后不能在程序中更改对象。 默认情况下,映射是不可变的,要使用可变映射,您必须使用scala.collection.mutable.Map类。

创建一个Scala地图 (Creating a Scala map)

There are different syntax to define mutable and immutable Scala maps,

定义可变和不可变的Scala映射有不同的语法,

Syntax for Creating Immutable maps:

创建不可变地图的语法:

map_name = Map(key_1 -> value_1, key_2 -> value_2, key_3 -> value_3)

Syntax for Creating mutable maps:

创建可变映射的语法:

map_name = Scala.collection.mutable.Map
            (key_1 -> value_1, key_2 -> value_2, key_3 -> value_3)

地图操作 (Operations on Map)

For a map in Scala, there are three basic operations on Scala maps:

对于Scala中地图,Scala地图上有三个基本操作

  1. keys: returns an iterable for all keys of the map. Syntax: map_name.keys

    keys :为地图的所有键返回一个可迭代的。 语法: map_name.keys

  2. values: return an iterable of all values of the map. Syntax: map_name.values

    values :返回地图所有值的可迭代值。 语法: map_name.values

  3. isEmpty: returns true if the map is empty otherwise false.

    isEmpty :如果映射为空,则返回true,否则返回false。

Some other important methods that are applied on map...

在地图上应用的其他一些重要方法...

1)创建一个空地图 (1) Creating an empty map)

Scala programming language allows you to create an empty map that does not contain any value pair. As per user requirements, new elements can be added to it.

Scala编程语言允许您创建一个不包含任何值对的空映射。 根据用户要求,可以向其中添加新元素。

Syntax:

句法:

var map_name = scala.collection.mutable.Map[datatype1 , datatype2]()

Example:

例:

object MyClass {
      def main(args: Array[String]) {
         var map = scala.collection.mutable.Map[Int, String]()
         println("The map created = "+map)
      }
   }

Output

输出量

The map created = Map()

2)向数组添加值 (2) Adding value to an array )

In Scala maps, there is an option to add new key-value pairs. += operator is used to add new values to the array.

在Scala映射中,有一个选项可以添加新的键值对。 + =运算符用于向数组添加新值。

Syntax:

句法:

var map_name += (key1 , value1)

Example:

例:

object MyClass {
      def main(args: Array[String]) {
         var map = scala.collection.mutable.Map[Int, String]()
         map += (1->"Akash")
         map += (2->"Ram", 3->"Kiran") // adding multiple values.
         println("The map created = "+map)
      }
   }

Output

输出量

The map created = Map(2 -> Ram, 1 -> Akash, 3 -> Kiran)

3)更改Scala地图的值 (3) Changing value of Scala maps)

By default, the values of Scala maps cannot be changed as it is by default immutable. But you can change values of mutable Scala maps. Using their key and assigning the new value.

默认情况下, Scala映射的值无法更改,因为默认情况下它是不可变的。 但是您可以更改可变的Scala映射的值。 使用其键并分配新值。

Syntax:

句法:

map_name.(key) = value

Example:

例:

object MyClass {
      def main(args: Array[String]) {
         var map = scala.collection.mutable.Map[Int, String]()
         map += (1->"Akash")
         // adding multiple values.
         map += (2->"Ram", 3->"Kiran") 
         println("The map created = "+map)
         map(2) = "Raju"
         println("The map after updating values = "+map)
      }
   }

Output

输出量

The map created = Map(2 -> Ram, 1 -> Akash, 3 -> Kiran)
The map after updating values = Map(2 -> Raju, 1 -> Akash, 3 -> Kiran)

4)访问地图的任何特定值 (4) Accessing any specific value of the map)

Any value of the Scala map can be accessed using its corresponding key.

Scala映射的任何值都可以使用其对应的键进行访问。

Syntax:

句法:

map_name.(key)


5)打印地图的所有键值(地图的迭代器) (5) Print all key-value of the map (iterator of the map))

In Scala, the user can iterate over all element of maps using a loop statement. To iterate over a map you can use foreach loop that is specially designed to handle data in structures like an array, list, map.

在Scala中,用户可以使用循环语句遍历地图的所有元素。 要遍历地图,可以使用foreach循环,该循环是专门设计用于处理数组,列表,地图等结构中的数据的。

Syntax:

句法:

map_name.keys.foreach{=> i
	//values can be extracted using map_name(key) method. 
}

Example:

例:

object MyClass {
      def main(args: Array[String]) {
         var map = scala.collection.mutable.Map[Int, String](1->"Akash" , 2->"Ram", 3->"Kiran" )
        map.keys.foreach{ i =>
         println("Keys :"+i+" Value :"+map(i));
        }
      }
   }

Output

输出量

Keys :2 Value :Ram
Keys :1 Value :Akash
Keys :3 Value :Kiran

6)在Scala映射中删除值 (6) Deleting value in Scala maps )

You can delete values in the map that are not required anymore. The -= operator in Scala map deals with the deletion of values from Map.

您可以删除地图中不再需要的值。 Scala映射中的-=运算符处理从Map中删除值的问题。

Syntax:

句法:

Map_name -= (key)

Example:

例:

object MyClass {
      def main(args: Array[String]) {
         var map = scala.collection.mutable.Map[Int, String](1->"Akash" , 2->"Ram", 3->"Kiran" )
        println("Intial map =\n"+map)
        map -= (1)
        println("Map after deletion =\n"+map)
      }
   }

Output

输出量

Intial map =
Map(2 -> Ram, 1 -> Akash, 3 -> Kiran)
Map after deletion =
Map(2 -> Ram, 3 -> Kiran)

7)检查钥匙是否在地图上 (7) Check if a key is in the map or not)

There is an option for the user to check for the value’s availability i.e. whether the value is in the map or not. The Scala contains() method deals with this.

用户可以选择一个选项来检查该值的可用性,即该值是否在地图中。 Scala contains()方法处理此问题。

Syntax:

句法:

map_name.cotains(key)

If the map contains the key, the function will return true otherwise false.

如果映射包含键,则该函数将返回true,否则返回false。

Example:

例:

object MyClass {
    def main(args: Array[String]) {
        var map = scala.collection.mutable.Map[Int, String](1->"Akash" , 2->"Ram", 3->"Kiran" )
        
        if(map.contains(1)){
            println("value "+map(1)+" exists in the map")
        }
        else {
            println("value does nor exist")
        }
        
        println("\nFor second value :\n")
        if(map.contains(5)){
            println("value "+map(5)+" exists in the map")
        }
        else {
            println("value does nor exist")
        }
    }
}

Output

输出量

value Akash exists in the map

For second value :

value does nor exist


翻译自: https://www.includehelp.com/scala/maps-in-scala.aspx

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值