Scala Associative Map
(Redirected from Scala Hash Map Data Structure)
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).
- AKA: Scala Hash, Scala Map.
- Context:
- It can extend an Scala AbstractMap Class.
- It can be:
- Example(s):
val engToDeu = Map(("dog","Hund"), ("cat","Katze"), ("rhinoceros","Nashorn"))
var capital = Map("US" -> "Washington", "France" -> "Paris")
- Counter-Example(s):
- See: Hash Key, Hash Function.
References
- http://www.scala-lang.org/api/current/index.html#scala.collection.mutable.HashMap
- http://docs.scala-lang.org/cheatsheets/
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
- (Odersky et al., 2010) ⇒ Martin Odersky, Lex Spoon, and Bill Venners. (2010). “Programming in Scala, 2nd edition." Artima. ISBN:0981531644
- QUOTE: One common characteristic of these languages, which is relevant for the example above, is that they each support an "associative map" construct in the syntax of the language. Associative maps are very useful because they help keep programs legible and concise. However, sometimes you might not agree with their "one size fits all" philosophy, because you need to control the properties of the maps you use in your program in a more fine-grained way. Scala gives you this fine-grained control if you need it, because maps in Scala are not language syntax. They are library abstractions that you can extend and adapt.