Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Big data, a good programmer, shares the mapping Map of Scala series.

Shulou Source: shulou.com Published: 2022-06-03 00:11:32 09月19日 Update

Good programmer big data learning route sharing Scala series of mapping Map first let's take a look at what is map in Scala, the hash table this data structure is called mapping.

1. In Scala, there are two kinds of Map, one is the Map under the immutable package, the content of the Map is immutable, and the other is the Map under the mutable package, the content of the Map is changeable.

Build an immutable map

Build using tuples

Build a mutable map

two。 To get and modify the values in the mapping, there are three ways to obtain the corresponding values in map according to the key. In particular, the getOrElse method is recommended.

Modify variable map information to traverse access map

Object MappingDemo {

Def main (args: Array [String]): Unit = {

/ / define to build a mutable map

Val scores = scala.collection.mutable.Map ("zhangsan"-> 90, "lisi"-> 80, "wangwu"-> 0)

/ / val scores2 = scala.collection.mutable.Map ("moumou"-> 50)

/ / modify the value of the corresponding key in map

Scores ("wangwu") = 100

/ / add a new key value to map

Scores ("zhaoliu") = 50 / / similar to scores.update ("zhangsan", 50)

Scores + = ("sunqi"-> 60, "qianba"-> 99)

/ / scores + + = scores2

/ / remove a key-value pair

Scores-= "zhangsan" / / similar to scores.remove ("zhangsan")

/ / scores-- = scores2 doesn't work?

/ / get the set merge traversal of the key

/ / it doesn't mean much? How to get a value by building

Val res = scores.keySet

For (elem

Tags: Variable data content method program programmer route learning not big not good information element meaning data structure way structure result hash first come recommend Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL NVidia Microsoft OPPO Reno MariaDB