如何在Scala中将List转换为Map?

在Scala中列出 (List in Scala)

List is a collection of immutable data of the same type. List represents a LinkedList in Scala.

列表是相同类型的不可变数据的集合。 列表表示Scala中的LinkedList。

Creating a list:

创建列表

Syntax to create a list in Scala,

在Scala中创建列表的语法,

    val list_name = List (element1, elemenet2, ...)

Example of list:

清单示例

    // List of integers
    List(3, 65, 1, 90, 54, 100)
    // List of strings
    List("scala", "C++", "JavaScript", "Python") 

Scala地图 (Maps in Scala)

Map is a collection of elements stored in the form of key-value pairs. They have unique keys that are used to retrieve the value related to the key.

Map是以键值对形式存储的元素的集合。 它们具有用于检索与键相关的值的唯一键。

Creating a Map in Scala:

在Scala中创建地图

    val map_name = Map(key1 -> value1, key2 -> value2, ....)

Example of Map:

地图示例

    Map(int -> string): Map(1 -> "Scala" , 2 -> "JavaScript", 3-> "Python")

在Scala中将列表转换为地图 (Convert List to Map in Scala)

In Scala, you can convert a list to a map in Scala using the toMap method.

在Scala中,您可以使用toMap方法将列表转换为Scala中的地图。

A map contains a set of values i.e. key-> value but a list contains single values. So, while converting a list to map we have two ways,

映射包含一组值,即key-> value,但列表包含单个值。 因此,在将列表转换为地图时,我们有两种方法:

  1. Add index to list.

    将索引添加到列表。

  2. Merge two lists, use one as key, and other as value.

    合并两个列表,将一个用作键,将另一个用作值。

通过向列表添加索引将列表转换为地图 (Converting list to map by adding index to the list)

We will add index as key values of the elements of the list. For this we will use the zipWithIndex method.

我们将添加索引作为列表元素的键值。 为此,我们将使用zipWithIndex方法。

Syntax:

句法:

    List_name .zipWithIndex.map{ case(v,i) => (i,)}.toMap

Program to convert list to map

程序将列表转换为地图

object MyObject {
    
    def main(args: Array[String]) {
        val progLang = List("C++", "JavaScript" , "Scala" , "Python")
        val map = progLang.zipWithIndex.map{ case (v,i) => (i,v) }.toMap
        println("The values of map : "+ map)
    }
}

Output:

输出:

The values of map : Map(0 -> C++, 1 -> JavaScript, 2 -> Scala, 3 -> Python)

Explanation:

说明:

In the above code, we have created a list progLang which contains names of programming languages. To convert this list to a map, we have used the toMap method. As the list has single value and map needs to values we have used indexes as key using the zipWithIndex method that adds index values starting from 0 to every element of the list in the map. Using these function we have created the Map with name map and printed its contents using the println method.

在上面的代码中,我们创建了一个列表progLang ,其中包含编程语言的名称。 为了将此列表转换为地图,我们使用了toMap方法。 由于列表具有单个值,并且映射需要值,因此我们使用zipWithIndex方法将索引用作键,该方法将索引值从0开始添加到映射中列表的每个元素。 使用这些功能,我们创建了带有名称映射的Map,并使用println方法打印了其内容。

通过合并两个列表将列表转换为地图 (Converting List to map by merging two list)

We can create a map by using two lists and merging them to create a map. For creation, we will add using one of the lists as key and other as value for the key-value pairs of the map.

我们可以通过使用两个列表并将它们合并以创建地图来创建地图。 为了进行创建,我们将使用列表之一作为键,另一些作为值添加到地图的键值对。

The list to be used as the key in the map should have unique elements and both the lists should have the same number of elements.

用作映射中键的列表应具有唯一元素,并且两个列表应具有相同数量的元素。

Syntax:

句法:

    map_name = (list1_name zip list2_name).toMap

Program to convert lists to map

程序将列表转换为地图

object MyObject {
    
    def main(args: Array[String]) {
        val myBikes = List("ThunderBird 350" , "YRF R3" , "S1000RR")
        val topSpeed = List(132 , 167 , 300)
        val bikeSpeed = (myBikes zip topSpeed).toMap
        println("The values of map : "+ bikeSpeed)
    }
}

Output:

输出:

The values of map : Map(ThunderBird 350 -> 132, YRF R3 -> 167, S1000RR -> 300)

Explanation:

说明:

In the above, code we have created two lists, myBike to store names of bikes and topSpeed to store the top speed of these bikes. Now, we have used the toMap method to convert the list to the map. To use one list as key and other as value, we have used the zip. Then we have stored the map to bikeSpeed variable and print its contents using println.

在上面的代码中,我们创建了两个列表, myBike用于存储自行车的名称, topSpeed用于存储这些自行车的最高速度。 现在,我们使用了toMap方法将列表转换为地图。 要将一个列表用作键,将另一个列表用作值,我们使用了zip 。 然后,我们将地图存储到bikeSpeed变量,并使用println打印其内容。

Points to remember:

要记住的要点:

  • While converting the elements of both lists should be the same otherwise the elements that are excess in the list will be left (will not be considered).

    虽然转换两个列表的元素时应相同,否则将保留列表中多余的元素(将不考虑)。

  • If the elements of the list to be used as keys are not unique the later element will be considered in the map and the match value for the first one will be discarded.

    如果要用作键的列表元素不是唯一的,则后一个元素将在映射中考虑,并且第一个元素的匹配值将被丢弃。

翻译自: https://www.includehelp.com/scala/how-to-convert-list-to-map-in-scala.aspx

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值