Scala Associative Map

From GM-RKB
Jump to navigation Jump to search

A Scala Associative Map is an associative array that is a Scala variable (based on the Scala Map library).



References

2013

  • (Melli, 2013-12-01) ⇒ Gabor Melli. (2011). “Scala Map Examples.".

2011

<lang Scala>// immutable maps
var map = Map(1 -> 2, 3 -> 4, 5 -> 6)
map(3) // 4
map = map + (44 -> 99) // maps are immutable, so we have to assign the result of adding elements
map.isDefinedAt(33) // false
map.isDefinedAt(44) // true</lang>
<lang scala>// mutable maps (HashSets)
import scala.collection.mutable.HashMap
val hash = new HashMap[Int, Int]
hash(1) = 2
hash += (1 -> 2)  // same as hash(1) = 2
hash += (3 -> 4, 5 -> 6, 44 -> 99)
hash(44) // 99
hash.contains(33) // false
hash.isDefinedAt(33) // same as contains
hash.contains(44) // true</lang>

2010