你需要.map
並獲得位置,這將給你Iterable[Location]
scala> val m = Map[String, Location]("prayagupd" -> Location("First hill", "England"),
"blankman" -> Location("Blank hill", "Blank States"))
m: scala.collection.immutable.Map[String,Location] = Map(prayagupd -> Location(First hill,England), blankman -> Location(Blank hill,Blank States))
scala> m.map { case (name, location) => location }
res10: scala.collection.immutable.Iterable[Location] = List(Location(First hill,England), Location(Blank hill,Blank States))
如果你需要的位置名稱,
scala> m.map { case (_, location) => location.name }
res14: scala.collection.immutable.Iterable[String] = List(First hill, Blank hill)
或者,你可以簡單地做.values
這也給出Iterable[Location]
,
scala> m.values
res2: Iterable[Location] = MapLike.DefaultValuesIterable(Location(First hill,England), Location(Blank hill,Blank States))
在地圖上'values'函數返回的所有值,從而無需使用'map'了點。 –