Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to initialize a Scala immutable hashmap with values?

Tags:

hashmap

scala

What's the syntax to set immutable hashmap contents on initialization?

For example, if I was willing to hardcode an array, I'd write:

val a = Array (0, 1, 2, 3)

What's the analogue for immutable hashmaps (say I want it to contain 0->1 and 2->3 pairs) (in Scala 2.8)?

like image 920
Ivan Avatar asked Oct 10 '10 03:10

Ivan


1 Answers

Do you mean something like this?

 scala> val m = collection.immutable.HashMap(0 -> 1, 2 -> 3) m: scala.collection.immutable.HashMap[Int,Int] = Map((0,1), (2,3))  scala> m.get(0) res0: Option[Int] = Some(1)  scala> m.get(2) res1: Option[Int] = Some(3)  scala> m.get(1) res2: Option[Int] = None 
like image 68
Arjan Blokzijl Avatar answered Sep 27 '22 20:09

Arjan Blokzijl