List in Scala
Val myFirstList = List[1,2]
myFirstList(0) = 1 // This will lead to compilation error
List is a immutable object
Map in Scala
We can have two types of Map: Immutable as well as Immutable. The default one is immutable since Scala promotes immutability, but yes, by including the import, we can define a mutable map also.
val mutableCasingName = scala.collection.mutable
.Map("camelCaseNamingPattern" -> "thanksForReading",
"snakeCaseNaming Pattern" -> "thanks_for_reading")
mutableCasingName += ("PascalCase" ->"ThanksForReading") //resulted in new variable
mutableCasingName += ("KebabCase" -> "thanks-for-reading") //resulted in new variable
val namingConventionExample =
Map("variablenaming" -> "readableName",
"classNaming" -> "ReadableClass",
"objectNaming" -> "ReadableObject",
"traitNaming" -> "ReadableTrait",
"methodNaming" -> "readableMethod"
)
namingConventionExample("methodNaming") = "hewgfhj" // Refer to demonstration for error
Output:
val mutableCasingName: scala.collection.mutable.Map[String,String] = HashMap(snakeCaseNaming Pattern -> thanks_for_reading, camelCaseNamingPattern -> thanksForReading)
val res0: mutableCasingName.type = HashMap(snakeCaseNaming Pattern -> thanks_for_reading, camelCaseNamingPattern -> thanksForReading, PascalCase -> ThanksForReading)
val res1: mutableCasingName.type = HashMap(KebabCase -> thanks-for-reading, snakeCaseNaming Pattern -> thanks_for_reading, camelCaseNamingPattern -> thanksForReading, PascalCase -> ThanksForReading)
val namingConventionExample: scala.collection.immutable.Map[String,String] = HashMap(traitNaming -> ReadableTrait, methodNaming -> readableMethod, objectNaming -> ReadableObject, variablenaming -> readableName, classNaming -> ReadableClass)
Demonstration of the above example: