val m: Map[String, List[(String, Set[String])]] =
Map("ABC" -> List(("ABC", Set("X"))))
// taking the set from the head of list, ignoring second "ABC"
val m1: Map[String, Set[String]] = m.view.mapValues(_.head._2).toMap
// Map(ABC -> Set(X))
// combining into union the sets from the whole list, ignoring second "ABC"
val m2: Map[String, Set[String]] =
m.view.mapValues(_.map(_._2).fold(Set())(_ union _)).toMap
// Map(ABC -> Set(X))
// ignoring first "ABC"
val m3: Map[String, Set[String]] = m.values.flatten.toMap
// Map(ABC -> Set(X))
1条答案
按热度按时间vmdwslir1#
例如,您可以使用
.mapValues
(
m3
由**@Philllightati**提供)https://www.scala-lang.org/api/current/scala/collection/immutable/Map.html
https://docs.scala-lang.org/overviews/collections-2.13/maps.html