{y{y[{_j9PVPhy{khwMy[MMy[M{_y{j9_qy[{_OVhy{@j[@[{|jh|hh[h{_Oy{MymM[{9k[zhMymM[MEyM[E{hMymMx[hh{_9{yyy{m[{m[y{hyX{My[MMy[M{VnynyoyyQ{8pwMyMyQ{w8pnwMywnmxMyMyQ{[[[okDcIEeErAX-~| 9864V 968V 9654V~~B 99G57VB~~ 9949494VC 69494VC~C 7968V~BC 664646F8W~-

Map.` These lands are all the same. Each hill, each stream and tree—the terrain repeats itself. With a landmark, you could map the area you have covered.`In Scala,` a map is the core dictionary type. We link values (places) from keys (or landmarks). Usually we have Strings and Ints, but any type can be used.`Initial example.` Here we build a String map. It has String keys and Int values. We initialize it with just two pairs. Map is immutable so a new map would be needed to add elements. `Get: `To get a value from a key in the "colors" map, we access the colors like a function. We pass the key as the argument.`Another example.` We can initialize a Map with a different syntax based on a list of pairs. Here we use a String to String map. We look up two animal Strings. `Add.` A Map is immutable so we cannot add an element to an existing map. But we can add a key-value pair to a new, copied map. Here we add a zebra in a new map creation statement. `Keys, values.` Here we create a map with String keys and Int values. We then access the keys and values properties to get collections of those elements. `Keys: `We use the foreach function on the keys collection (which is an iterable) and use println on each key.`Values: `Here we use an imperative for-loop to call println on each value in the Map.`For `for-scala`Get, getOrElse.` With get, an option is returned. If the value exists in the Map, isDefined will return true on that option. We can then call get() on the option. `Option `option-scala`Get: `This returns an option, not the value type of the Map. We must always test the option.`GetOrElse: `This lets us provide a default value that is returned if the key does not exist in the Map. Here we use 0 as a default.`Equals.` With this method we test for structural equality of two maps. The order of the entries in the maps is not important. But the keys and values must be the same. `We find map1 and map2 are equal because their key-value pairs are the same. But map3 has a different value so it is not equal.`WithDefaultValue.` A map can have a default value or function. We call withDefaultValue to specify a default value—in this program we use a default integer of -1. `So: `When we do a lookup on the map that does not find an existing key, the default value is returned.`WithDefault: `This method returns a default value based on a function. We can pass a lambda expression to the withDefault method.`The map does not contain the key "bear" so the final look up in the program returns the value -1.`Map performance.` A Map is a good optimization. Here we have three elements "abc" and we store them in a Map and a List. We search for one element in the collections. `Details: `We search the Map with a string argument. With the List we use a for-loop to search.`List `list-scala`Result: `Finding element "c" in the Map is faster than in the List. The fast lookup time helps this benchmark.`Immutable, research.` Immutable types, such as scala.collection.immutable.Map, are key in Scala. This Map is a generic trait. It indicates an immutable map. `For this Map, elements cannot be added, but new maps can be created. We add elements by creating new maps.`Quote: `A generic trait for immutable maps. Concrete classes have to provide functionality for the abstract methods in Map (Scala Standard Library).`To summarize:` we use the Map type to associate keys with values. This provides fast, hashed lookups. Maps solve many performance problems, and can even simplify code by removing duplicates.

WXY YXUse simple map initialization syntax. Yval weightsXYMapY(Y"cat"Y -> 10, Y"elephant"Y -> 200000)Y XLook up animal weights. Yval weightXweights(Y"elephant"Y) Xln(weight) Y 200000Y YXCX map of animalsXcolors. XHas X keysXX Xs. Yval colorsXYMapY(("bird", "blue"), ("fox", "red"))Y XXX Xthis key. Yval X1Xcolors(Y"bird"Y) Xln(X1) val X2Xcolors(Y"fox"Y) Xln(X2) Y blue redY YXCX an immutable map. Yval zooXYMapY("frog" -> 1, "lion" -> 1)Y XAdd a pairXthe map. XThis cXs a Xmap. Yval allXzoo Y+Y (Y"zebra"Y -> 1) Xln(all) Y Map(frog -> 1, lion -> 1, zebra -> 1)Y YXCX map of X keysXInt Xs. Yval idsXYMapY(("abc", 10), ("def", 20))Y XUse X functionXX each key. Yids.YkeysY.X(Xln(_))Y XUse X-loopXiterate over all Xs. YXY (X <- ids.YXsY) { WXln(X) } Y abc def 10 20Y YXCX a X, Int map. Yval sizesXYMapY(("Medium", 2), ("Large", 4))Y XThis key does not exist, so the option is not defined. Yval X1Xsizes.YgetY(Y"Small"Y) X!X1.isDefined) { WXln("Not defined") }Y XThis key exists. XXand X the option's Xernal X. Yval X2Xsizes.YgetY(Y"Large"Y) XX2.isDefined) { WXln(X2) Wval XXX2.get WXln(X) }Y XUse a default X if the key does not exist. Yval X3Xsizes.YgetOrElseY(Y"Small"Y, 0) XX3X0) { WXln("Zero, else X") } Y Not defined Some(4) 4 Zero, else XY YXCX three maps. XThe first two are equal but have different orders. XWThe third one has different entries. Yval map1XMap((10, true), (20, false)) val map2XMap((20, false), (10, true)) val map3XMap((20, true), (10, true))Y XThese two maps are structurally equal. YXmap1.YequalsY(map2)) { WXln(Y"Maps equal"Y) }Y XNot equal. YX!map1.YequalsY(map3)) { WXln(Y"Maps not equal"Y) } Y Maps equal Maps not equalY val animalMapXYMapY(("cat", 10), ("bird", 5))Y XThe default X is now negative 1. Yval animalMapDefaultXanimalMap.YwithDefaultXY(-1);Y XUse map with default. Yval X1XanimalMapDefault(Y"cat"Y) Xln(X1) val X2XanimalMapDefault(Y"bear"Y) Xln(X2) Y 10 -1Y YXMapXX used. Yval lookupXMap(("a", 1), ("b", 1), ("c", 1)) val XXX("a", "b", "c") var totalX0 val t1XX.currentTimeMillis()Y XVersion 1: look up XXa map. YX(i <- 0 until 10000000) { Wval XXlookup(Y"c"Y) Wtotal += X } val t2XX.currentTimeMillis()Y XVersion 2: search XXXa X. YX(i <- 0 until 10000000) { Wvar XX0 WX(v <- X) { WWXvXY"c"Y) { WWWXX1 WW} W} Wtotal += X } val t3XX.currentTimeMillis()Y XResults. YXln(total) Xln(t2 - t1) Xln(t3 - t2) YResultsY 20000000 Y 77 msY,WMap Y140 msY,WXY

(+<61^V`2creates Mapcreates Map with pair syntaxadds key, value to mapgets keys, valuesget, getOrElseequalswithDefaultValuetimes Map and List