val scan2 = new scala.collection.mutable.HashMap[String, Int]
var scan2 = new scala.collection.immutable.HashMap[String, Int]
which one I should prfer or better, for me both doing the same job. it is bit confusing me. I am new to Scala.
val scan2 = new scala.collection.mutable.HashMap[String, Int]
var scan2 = new scala.collection.immutable.HashMap[String, Int]
which one I should prfer or better, for me both doing the same job. it is bit confusing me. I am new to Scala.
If you are coming over from Java, you may find the mutable version more familiar, and easier to use when rewriting Java code in Scala. For the longer term, however, I (and others) recommend getting used to using and working with the immutable forms of the collection classes wherever possible. Working with immutable data (the immutable collections, using 'val', rather than 'var', etc.) is more functional (as in 'functional programming' style) in nature, which is a strength of Scala worth exploring. For example, it works better in concurrent situations (not having to worry about locking/synchronizing access to your collection to stop it being modified unexpectedly and so on) and enables better reasoning about how your code works. You shouldn't find it too hard to rewrite your classes to work with the immutable collection types - think in terms of how you use Java's String class (which is also immutable).