0
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.

Philip Puthenvila
  • 512
  • 1
  • 8
  • 19

1 Answers1

1

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).

Shadowlands
  • 14,994
  • 4
  • 45
  • 43
  • thanks for the answer.in a web application point of view an immutable map is of not much use, all the time we have to modfiy the collection by adding or removing. so what I understood is if I want a immutable collection I should use imutable collection with val key word. then I cannot modify. so how you approach this type fo problem. – Philip Puthenvila Aug 01 '13 at 07:11