我想宣告一個型別的變數AbstractSeq[(String, Integer)],它將存盤一個對串列。然后根據程式流程,我可能會分配一個ArrayBuffer[(String, Integer)]給它,或者一個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
但我收到以下錯誤:
found : scala.collection.immutable.Map[String,Integer]
required: scala.collection.immutable.AbstractSeq[(String, Integer)]
我的問題:為什么會出現此錯誤?不Map只是一個 AbstractSeq 對,因此我應該能夠做到這一點?
我嘗試過的:我搜索了這個錯誤和相關的關鍵字,但沒有找到我的問題的答案。
為什么我要問:我試圖了解 Scala 中不同 Collection 型別之間的關系。哪些集合型別可以分配給哪些型別的變數,為什么?
uj5u.com熱心網友回復:
因為Map[K, V]不是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]
檔案有集合概述,可以闡明不同集合型別之間的一些關系。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/422302.html
標籤:
上一篇:scala型別擦除-匹配案例類
