问题描述
我有这样的东西:
val myMap: Map[Int, Seq[Int]] = Map(1 -> (1, 2, 3), 2 -> (2, 3, 4), 3 -> (3, 4, 5), 4 -> (4, 5, 6))
我正在尝试找到一种方法,以将所有键及其公共元素映射到它们的顺序.
I am trying to find a way to relate all the keys and their common elements in the sequence they are mapped to.
例如:
1和2分享(2,3)
1 and 2 share (2, 3)
1和3分享(3)
2和3分享(3,4)
2 and 3 share (3, 4)
2和4分享(4)
3和4分享(4,5)
3 and 4 share (4, 5)
我怀疑我需要使用 intersect
,但不确定如何解决该问题.我是scala和函数式编程的新手,并且需要一些帮助来开始这一工作.我知道可能有更简便的方法可以执行此操作,但是,我试图只坚持使用scala.
I suspect I need to use intersect
but I am not sure how to go about the problem. I am brand new to scala and functional programming and need a little help getting started on this. I know there are probably easier ways to do this with spark, however, I am trying to stick just to scala.
任何帮助将不胜感激!
推荐答案
您希望如何返回结果?您是否只想将它们打印到STDOUT?
How do you want the results returned? Do you just want to print them to STDOUT?
myMap.keys.toList.combinations(2).foreach{ case List(a,b) =>
println(s"$a,$b --> ${myMap(a) intersect myMap(b)}")
}
这篇关于在序列图中查找常见元素-Scala的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!