如何获得哈希图 Scala 值的可能对组合
How to get the possible pair combinaisons of values of a hashmap Scala
我有一个 hashmap 如下:
val hm : HashMap[Int, List[String]] =
HashMap(
1 -> List("Eat", "Drink","Sleep", "work"),
2 -> List("Eat", "Sleep","Dance"),
3 -> List("Write", "Print","Dance")
)
我想检索此散列映射值的可能对,return 分别在列表中检索每一对
我将组合函数用作
hm.mapValues(_.combinations(2).toList)
结果是:
Map(1-> List(List(Eat, Drink), List(Eat, Sleep), List(Eat, work), List(Drink, Sleep), List(Drink, work), List(Sleep, work)), 2-> List(List(Eat, Sleep), List(Eat, Dance), List(Sleep, Dance)), 3 -> List(List(Write, Print), List(Write, Dance), List(Print, Dance)))
但预期的结果应该是三个列表
List(List("Eat", "Drink","Sleep", "work"),List("Eat", "Sleep","Dance"))
List( List("Eat", "Drink","Sleep", "work"),List("Write", "Print","Dance"))
List(List("Eat", "Sleep","Dance"), List("Write", "Print","Dance"))
我错过了什么
仅使用地图的值:
hm.values.toList.combinations(2).toList
我有一个 hashmap 如下:
val hm : HashMap[Int, List[String]] =
HashMap(
1 -> List("Eat", "Drink","Sleep", "work"),
2 -> List("Eat", "Sleep","Dance"),
3 -> List("Write", "Print","Dance")
)
我想检索此散列映射值的可能对,return 分别在列表中检索每一对 我将组合函数用作
hm.mapValues(_.combinations(2).toList)
结果是:
Map(1-> List(List(Eat, Drink), List(Eat, Sleep), List(Eat, work), List(Drink, Sleep), List(Drink, work), List(Sleep, work)), 2-> List(List(Eat, Sleep), List(Eat, Dance), List(Sleep, Dance)), 3 -> List(List(Write, Print), List(Write, Dance), List(Print, Dance)))
但预期的结果应该是三个列表
List(List("Eat", "Drink","Sleep", "work"),List("Eat", "Sleep","Dance"))
List( List("Eat", "Drink","Sleep", "work"),List("Write", "Print","Dance"))
List(List("Eat", "Sleep","Dance"), List("Write", "Print","Dance"))
我错过了什么
仅使用地图的值:
hm.values.toList.combinations(2).toList