I want to declare a variable of type AbstractSeq[(String, Integer)] which will store a list of pairs. Then depending on the program flow, I might assign an ArrayBuffer[(String, Integer)] to it, or a Map[String, Integer]:
val map: Map[String, Integer] = Map("Alice" -> 10, "Bob" -> 3, "Cindy" -> 8)
var lst: AbstractSeq[(String, Integer)] = List.empty
...
lst = map # compile error
But I get the following error:
found : scala.collection.immutable.Map[String,Integer]
required: scala.collection.immutable.AbstractSeq[(String, Integer)]
My question: Why do I get this error? Isn't Map just an AbstractSeq of pairs, and thus I should be able to do this?
What I've tried: I have googled around for this error and related keywords, and haven't found an answer to my question.
Why I'm asking: I'm trying to understand the relationship between different Collection types in Scala. Which Collection types can be assigned to variables of which types, and why?
CodePudding user response:
Because Map[K, V] is not an AbstractSeq:
trait Map[K, V] extends Iterable[(K, V)] with collection.Map[K, V] with MapOps[K, V, Map, Map[K, V]] with MapFactoryDefaults[K, V, Map, Iterable]
Docs have collection overview which can shed some light one relationships between different collection types.
