How to declare a Empty Scala Map

0 votes
Can anyone explain how to declare a Empty Scala Map in Scala? Can anyone provide a simple example to this?
Jul 29, 2019 in Apache Spark by Tarinder
374 views

1 answer to this question.

0 votes

Hi,

  • You can either declare an empty Scala map or one with values. Here is an example:
scala> var m:Map[String,Int]=Map()

m: Map[String,Int] = Map()

Here, you must include the type annotation so it can assign proper types to the variables.

  • Declaring a Scala Map with values

When you provide values, Scala will use those to infer the type of variables. So, we don’t need to include the type annotations.

Here is an example:

scala> var m=Map("Niti"->0,"Giti"->1)

m: scala.collection.immutable.Map[String,Int] = Map(Niti -> 0, Giti -> 1)

Now to add key-value pair you need to follow this:

scala> m+=("Ruchi"->2)

scala> m

res1: scala.collection.immutable.Map[String,Int] = Map(Niti -> 0, Giti -> 1, Ruchi -> 2)

answered Jul 29, 2019 by Gitika
• 65,870 points

edited Jul 29, 2019 by Gitika

Related Questions In Apache Spark

0 votes
1 answer

How to get ID of a map task in Spark?

you can access task information using TaskContext: import org.apache.spark.TaskContext sc.parallelize(Seq[Int](), ...READ MORE

answered Nov 20, 2018 in Apache Spark by Frankie
• 9,810 points
1,744 views
0 votes
1 answer

How to declare Scala array?

Hi, We can declare Scala arrays in two ...READ MORE

answered Jul 25, 2019 in Apache Spark by Gitika
• 65,870 points
155 views
0 votes
1 answer

How to append a list in Scala?

Hey, For this purpose, we use the single ...READ MORE

answered Jul 26, 2019 in Apache Spark by Gitika
• 65,870 points
3,169 views
0 votes
1 answer

How to declare sets with value in Scala?

Hey, You can follow this code: scala> var s=Set(1,4,4,3) s: ...READ MORE

answered Jul 30, 2019 in Apache Spark by Gitika
• 65,870 points
125 views
+1 vote
2 answers
+1 vote
1 answer

Hadoop Mapreduce word count Program

Firstly you need to understand the concept ...READ MORE

answered Mar 16, 2018 in Data Analytics by nitinrawat895
• 11,380 points
7,232 views
0 votes
1 answer

hadoop.mapred vs hadoop.mapreduce?

org.apache.hadoop.mapred is the Old API  org.apache.hadoop.mapreduce is the ...READ MORE

answered Mar 16, 2018 in Data Analytics by nitinrawat895
• 11,380 points
1,176 views
+2 votes
11 answers

hadoop fs -put command?

Hi, You can create one directory in HDFS ...READ MORE

answered Mar 16, 2018 in Big Data Hadoop by nitinrawat895
• 11,380 points
53,077 views
0 votes
1 answer

How to search for a Key in a Scala Map?

Hey, The Map.contains() method will tell you if ...READ MORE

answered Jul 29, 2019 in Apache Spark by Gitika
• 65,870 points
138 views
0 votes
2 answers

How to execute a function in apache-scala?

Function Definition : def test():Unit{ var a=10 var b=20 var c=a+b } calling ...READ MORE

answered Aug 5, 2020 in Apache Spark by Ramkumar Ramasamy
194 views