Tuesday, September 13, 2016

Map Example : Mutable Map, Immutable Map, HashMap etc...

  1. We have the option to select either Mutable or Immutable Map #90
  2. By default, Immutable set will be used if we do not Specificy the Mutuable Map package
  3. Package
  4.     scala.collection.mutable
  5.     scala.collection.immutable
/*
    Immutable Map example #90
    (Default Implementation)
*/    
// '->' is a method that returns a Tuple of key & value
val m1 = Map( "math" -> "80", "english" -> "90")
val m2 = Map(("math", "80"), ("english", "90"))
m2("some") = "value"

/*
    Mutable Map example #90
*/    
import scala.collection.mutable
val m1 = mutable.Map()
val m1 = mutable.Map[String, String]()
m1("science") = "80"
m1("math") = "85"
m1

/*
    HashMap example
*/
import scala.collection.immutable
val m1 = immutable.Map( "math" -> "80", "english" -> "90")