如何在java代码中设置scala隐式值



我有一个scala类,其定义如下:

object EigenvectorCentrality extends VertexMeasure[Double]{
override def compute[VD:ClassTag, ED:ClassTag](graph: Graph[VD, ED],vertexMeasureConfiguration: VertexMeasureConfiguration[VD,ED])(implicit num:Numeric[ED]): Graph[Double, ED] = computeEigenvector(graph,vertexMeasureConfiguration)
}

现在我想从Java代码中调用这个类的计算函数,但我不知道如何设置计算函数的Latest Parameter(Numeric[ED](。我把这个函数称为:

EigenVectorCentrality.compute(mygraph,mygraph.vertices.vdtag,mygraph.vertices.vdtag,Numeric.Int)

但是得到这个错误:Could not find Implicit value for parameter num : Numeric[ED]

这个类与Spark Graphx有关。你知道java中如何设置scala的隐式变量吗?

什么是ED?您应该传递Scala在执行隐式解析时选择的相同对象。假设EDDouble,您应该传递DoubleIsFractional对象,该对象在Java中被写成DoubleIsFractional$.MODULE$

EigenVectorCentrality.compute(mygraph,mygraph.vertices.vdtag,mygraph.vertices.vdtag,DoubleIsFractional$.MODULE$)

如果EDInt,则应该是IntIsIntegral$.MODULE$

如果ED是基元类型,那么映射Scala/Java基元类型就有额外的麻烦。假设EDint(即Scala中的Int(,那么在Java中不能有Numeric<int>类型的值,只能有Numeric<Integer>,但默认情况下Scala中没有定义这种隐式值。

最新更新