Question for instantiating of Seq

scala> val xx = Set(1,2,3)
val xx: scala.collection.immutable.Set[Int] = Set(1, 2, 3)

scala> val yy = Seq(1,2,3)
val yy: Seq[Int] = List(1, 2, 3)

scala> val zz = Map(1 -> 2, 3 -> 4)
val zz: scala.collection.immutable.Map[Int,Int] = Map(1 -> 2, 3 -> 4)

As you see above, only Seq is instantiated as List.
But Map not instantiated as HashMap, Set not instantiated as HashSet.
Why this happens? Thanks.

If you look at the class of the created map you’ll notice that it is in fact scala.collection.immutable.Map$Map2 in this case. This is because immutable maps of 0, 1, 2, 3 and 4 elements all have their own special implementation. As for the output stating it as scala.collection.immutable.Map[Int,Int], this is caused by Map2 inheriting scala.collection.Map.stringPrefix (which sets it to Map).

1 Like