Maps

A map is an Iterable consisting of pairs of keys and values (also named mappings or associations). A map is also one of the most widely used connections as it can be used to hold basic datatypes. For example:

scala> Map(1 -> 2)
res7: scala.collection.immutable.Map[Int,Int] = Map(1 -> 2) scala> Map("X" -> "Y")
res8: scala.collection.immutable.Map[String,String] = Map(X -> Y)

Scala's Predef object offers an implicit conversion that lets you write key -> value as an alternate syntax for the pair (key, value). For instance, Map("a" -> 10, "b" -> 15, "c" -> 16) means exactly the same as Map(("a", 10), ("b", 15), ("c", 16)), but reads better.

Moreover, a Map can be simply considered a collection of Tuple2s:

Map(2 -> "two", 4 -> "four")

The preceding line will be understood as:

Map((2, "two"), (4, "four"))

In the example, we can state that using Map a function can be stored, and this is the whole point of functions in a Functional Programming language: they are first-class citizens and can be used anywhere.

Suppose you have a method for finding the max element in an array as follows:

var myArray = range(5, 20, 2)
def getMax(): Int = {
// Finding the largest element
var max = myArray(0)
for (i <- 1 to (myArray.length - 1)) {
if (myArray(i) > max)
max = myArray(i)
}
max
}

Now, let's map it such that using the Map the method can be stored:

scala> val myMax = Map("getMax" -> getMax()) 
scala> println("My max is: " + myMax )

Let's another of using maps as follows:

scala> Map( 2 -> "two", 4 -> "four")
res9: scala.collection.immutable.Map[Int,String] = Map(2 -> two, 4 -> four)
scala> Map( 1 -> Map("X"-> "Y"))
res10: scala.collection.immutable.Map[Int,scala.collection.immutable.Map[String,String]] = Map(1 -> Map(X -> Y))

The following is a detailed example to demonstrate Map functionality:

package com.chapter4.CollectionAPI
import Array._

object MapExample {
var myArray = range(5, 20, 2)

def getMax(): Int = {
// Finding the largest element
var max = myArray(0)
for (i <- 1 to (myArray.length - 1)) {
if (myArray(i) > max)
max = myArray(i)
}
max
}

def main(args: Array[String]) {
val capitals = Map("Ireland" -> "Dublin", "Britain" -> "London",
"Germany" -> "Berlin")

val temp: Map[Int, Int] = Map()
val myMax = Map("getMax" -> getMax())
println("My max is: " + myMax )

println("Keys in capitals : " + capitals.keys)
println("Values in capitals : " + capitals.values)
println("Check if capitals is empty : " + capitals.isEmpty)
println("Check if temp is empty : " + temp.isEmpty)

val capitals1 = Map("Ireland" -> "Dublin", "Turkey" -> "Ankara",
"Egypt" -> "Cairo")
val capitals2 = Map("Germany" -> "Berlin", "Saudi Arabia" ->
"Riyadh")

// Map concatenation using ++ operator
var capitalsConcatenated = capitals1 ++ capitals2
println("capitals1 ++ capitals2 : " + capitalsConcatenated)

// use two maps with ++ as method
capitalsConcatenated = capitals1.++(capitals2)
println("capitals1.++(capitals2)) : " + capitalsConcatenated)

}
}

You will get the following output:

My max is: Map(getMax -> 19)
Keys in capitals : Set(Ireland, Britain, Germany)
Values in capitals : MapLike(Dublin, London, Berlin)
Check if capitals is empty : false
Check if temp is empty : true
capitals1 ++ capitals2 : Map(Saudi Arabia -> Riyadh, Egypt -> Cairo, Ireland -> Dublin, Turkey -> Ankara, Germany -> Berlin)
capitals1.++(capitals2)) : Map(Saudi Arabia -> Riyadh, Egypt -> Cairo, Ireland -> Dublin, Turkey -> Ankara, Germany -> Berlin)

Now, let's take a quick overview of using option in Scala; this is basically a data container that can hold data.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.59.160.92