GraphX - 如何将数组[(VertexId,Array(VertexId)]转换为Set(VertexId)

时间:2017-03-05 07:25:55

标签: arrays scala graph set

鉴于VertexID,我将所有直接连接的顶点连接到那个顶点? (IE,只有一个边缘)通过使用以下代码执行collectNeighborsId和查找:

val ID = 20
val res = graph.collectNeighborIds(EdgeDirection.Out).lookup(ID)

结果类是一个ArrayBuffer(Array(Long),如下所示:

ArrayBuffer(Array(10006, 10009, 10015, 10017, 10025, 10028, 10030, 10037, 10041, 10043, 10046, 10049, 10055, 10056, 10057, 10058, 10059, 10060, 10061, 10068, 10070, 10081, 10082, 10087, 10096, 10101, 10105, 10108, 10111, 10113, 10115, 10119, 10125, 10127, 10129, 10132, 10136, 10137, 10141, 10150, 10152, 10153, 10156, 10158, 10163, 10166, 10167, 10171, 10172, 10173, 10175, 10183, 10186, 10187, 10192, 10193, 10197, 10198, 10201, 10209, 10212, 10213, 10214, 10223, 10226, 10228, 1367, 1654, 1670, 2034, 2146, 2481, 58776, 58778, 58788, 58789))

我需要将它转换为Set [Long] .. 我试图通过使用toSet转换它..但它仍然导致Array(Set [Long])

1 个答案:

答案 0 :(得分:0)

使用flatten

scala> val a: Array[Array[Long]] = Array(Array(1, 2), Array(3, 4))
a: Array[Array[Long]] = Array(Array(1, 2), Array(3, 4))

scala> a.flatten.toSet
res1: scala.collection.immutable.Set[Long] = Set(1, 2, 3, 4)