Scala Collections : Map and Tuple with Code Examples

Same as Java Map, Scala Map is the collection(Iterable) of various key and value pairs in Scala Collections. All keys in Map are unique and associated with a value. Same as List and Set, Scala Maps are also having two types immutable and mutable.Here is the Syntax for creating Map in Scala,

Map(key1 -> value1, key2 -> value2,..., keyN -> valueN)

By default Scala uses Map class from scala.collection.immutable package. In case you want to use Mutable Map, you need use it from scala.collection.mutable package.

Let’s have some code examples of immutable Map.

//Creating Map with Values
var cityPopulationMap = Map("New York" -> 8491079, "Los Angeles" -> 3928864)

//Adding new values to Map
cityPopulationMap += ("Chicago" -> 2722389)
cityPopulationMap += ("Houston" -> 2239558)
cityPopulationMap += ("Philadelphia" -> 1560297)
cityPopulationMap += ("Phoenix" -> 1537058)

//Get new values to Map
println("Population of Chicago is " + cityPopulationMap("Chicago"))

//Remove values from Map
cityPopulationMap -= "Phoenix"
cityPopulationMap -= ("Houston", "Philadelphia")

//Print values in Map
println(cityPopulationMap)

You can also run above examples using Mutable Map.

What are Tuples in Scala?

Tuple is the collection class in Scala which can hold multiple values with same or different types together.

For example if you want a common reference for student name and roll-number you can create a tuple using syntax (studentName, studentRollNo)

scala> val student = ("Andrew", 1201)
student: (java.lang.String, Int) = (Andrew,1201)

//Let's check what is the class type
scala> student.getClass
res0: java.lang.Class[_ <: (java.lang.String, Int)] = class scala.Tuple2

Similarly you can create tuple for address information

scala> val address = ("12th Street East", "Green Cove Springs", "FL 32043")
address: (java.lang.String, java.lang.String, java.lang.String) = (12th Street East,Green Cove Springs,FL 32043)

scala> address.getClass
res1: java.lang.Class[_ <: (java.lang.String, java.lang.String, java.lang.String)] = class scala.Tuple3

If you have observed in above two tuples the class type of student is scala.Tuple2 and class type of address is scala.Tuple3. These is how you can extend your tuple up-to 22 values at a time and you respective class type will change from Tuple2 to Tuple22 in Scala.

How to access values of Tuple in Scala?

You can access the the respective elements of tuple using _1, _2 up-to _22. For example in our above examples if we want to access the student name and roll number which are on index 1 and 2 respectively, you can use following syntax.

//Accessing student name
val studentName = student._1
println("Student Name: " + studentName)

//Accessing respective from address elements
val addressStreet = address._1
val addressLandmark = address._2
val addressPin = address._3
println("Address: " + addressStreet + ", " + addressLandmark + ", " +  addressPin)

//Output
//Student Name: Andrew
//Address: 12th Street East, Green Cove Springs, FL 32043

This is how we can assign values of tuples to multiple variables in one line.

val (studentName, studentRollNo) =  student
val (addressStreet, addressLandmark, addressPin) =  address
println("Student Name: " + studentName)
println("Student Roll No.: " + studentRollNo)
println("Address: " + addressStreet + ", " + addressLandmark + ", " +  addressPin)
//Output
//Student Name: Andrew
//Address: 12th Street East, Green Cove Springs, FL 32043

Now we just want to extract student name and pin information. This is how we can do that.

val (studentName, _) =  student
val (_, _, addressPin) =  address
println("Student Name: " + studentName)
println("Pin: " + addressPin)
//Output
//Student Name: Andrew
//Pin: FL 32043

I hope you enjoyed this article. Please give your valuable comments on this.

References

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>