Collections · 26 of 42

Maps

  • Maps are constructed simply using Map(key1 -> value1, key2 -> value2, ...)
  • The Map can contain mixed types, but the final type of the Map keys / values will be the lowest common denominator type
  • The default Map is Predef.Map which points to scala.collection.immutable.Map
  • As it currently stands, Map implementation up to size of 4 has a specific class Map1, Map2, Map3, Map4, beyond that, it uses an immutable HashMap
  • You can’t have duplicate keys, adding a key value pair whose key already exists overwrites the value
  • Order of iteration is not guaranteed to be consistent
val map1 = Map("one" -> 1, "two" -> 2, "three" -> 3)
//Map of type Map[String, Int]
val map2 = Map(1 -> "one", "2" -> 2.0, 3.0 -> false)
//Map of type Map[Any, Any]

import collection.mutable
val mmap = mutable.HashMap("a" -> 1, "b" -> 2, "c" -> 3)
//the "mutable version" of Map

//Maps remove duplicate keys:
println(Map("a" -> 1, "a" -> 2))

//Get items using map(key)
val one = map1("one")

//NoSuchElementException will be thrown if key doesn't exist!
//e.g. this code: val fourExists = map1("four")
//throws NoSuchElementException: key not found: four
//the get method returns an Option, which will be explained later
val fourExistsOption = map1.get("four")

println(one)
println(fourExistsOption.isDefined)

//You can set / modify items using map(key) = value
mmap("d") = 4
println(mmap)

//Concatenation using the ++ operator
//(removes duplicate keys, order not guaranteed)
//note: map2 (whose key type is Any) goes first,
//because ++ keeps the key type of the map on its left
val concatenated = map2 ++ map1 ++ mmap
println(concatenated)
//Concatenation doesn't modify the maps themselves
println(map1)

//Removing elements (mutable Maps only)
mmap -= "c"
println(mmap)

//Adding elements (mutable Maps only)
mmap += "e" -> 5
mmap ++= Map("f" -> 6, "g" -> 7)

println(mmap)

//Find
val personMap = Map(("Alice",1), ("Bob",2), ("Carol",3))
def findByName(name:String) = personMap.getOrElse(name, 4)
val findBob = findByName("Bob")
val findEli = findByName("Eli")

println(findBob)
println(findEli)

Contents

Scala basics

  1. Overview
  2. Scalculator
  3. Operators are methods
  4. Variables
  5. Final variables
  6. Printing values
  7. String interpolation
  8. String formatting
  9. Useful operations
  10. Method definition
  11. Method definition 2
  12. Method definition 3
  13. Anonymous functions
  14. Anonymous functions 2
  15. Return multiple values
  16. Declare multiple variables
  17. Assign multiple variables
  18. Loops using while
  19. Loops using for
  20. Loops without loops
  21. If
  22. Match as a switch
  23. Arrays
  24. Lists
  25. Sets
  26. Maps
  27. Mutable collections
  28. Collections: accessing elements
  29. Collections: concatenation
  30. Mutable collection operations
  31. Immutable collections with var
  32. Collections: useful methods
  33. Classes
  34. Classes, continued

What's new in Scala 3

  1. Scala 3: what changed
  2. Optional braces & new control syntax
  3. Top-level definitions & @main
  4. Enums & ADTs
  5. Extension methods
  6. given & using
  7. Union & intersection types
  8. Smaller niceties