我有这样的列表,并想将它们转换为地图
val myList = List(
"IDENTIFIER, a, b, c",
"IDENTIFIER, d, e,f",
"INFORMATION, a, b, c",
"INFORMATION, d, e, f"
)
预期输出:
Map[String,List[String]] = Map(IDENTIFIER -> List(a b c, d e f), INFORMATION -> List(a b c, d e f))`
答案 0 :(得分:-1)
提供列表:
val myList = List(
"IDENTIFIER, a, b, c",
"IDENTIFIER, d, e, f",
"INFORMATION, a, b, c",
"INFORMATION, d, e, f"
)
使用
myList.map(_.split(",")).groupBy(_.head).mapValues(_.map(_.tail.mkString(" ").trim))
在Scala REPL中:
scala> myList.map(_.split(",")).groupBy(_.head).mapValues(_.map(_.tail.mkString(" ").trim))
res91: scala.collection.immutable.Map[String,List[String]] = Map(IDENTIFIER -> List(a b c, d e f), INFORMATION -> List(a
b c, d e f))
scala>