网上代码 把Javamap转为scala的map代码
scala.collection.mutable.Map<String, String> scalaMap = JavaConverters.mapAsScalaMapConverter(map).asScala();Object objMap = Map$.MODULE$.<String, String>newBuilder().$plus$plus$eq(scalaMap.toSeq());Object BuildResObjMap = ((scala.collection.mutable.Builder) objMap).result();scala.collection.immutable.Map<String, String> targetScalaMap = (scala.collection.immutable.Map) BuildResObjMap;return targetScalaMap;
我自身是用scala2.11.8, jdk8, 同样代码在本机报错如下:
toSeq()方法就一个方法还会不明确也是奇葩, 遂去翻源码,然后发现seq和其他集和对象关系
那么算了不去转到seq了, 我转到他的子类总改明确吧, 遂把代码改成以下两种方式分别实验:
public static scala.collection.immutable.Map<String, String> javaMapConvertToScalaMap(Map<String, String> map) {scala.collection.mutable.Map<String, String> scalaMap = JavaConverters.mapAsScalaMapConverter(map).asScala();Object objMap = Map$.MODULE$.<String, String>newBuilder().$plus$plus$eq(scalaMap.toList());Object BuildResObjMap = ((scala.collection.mutable.Builder) objMap).result();scala.collection.immutable.Map<String, String> targetScalaMap = (scala.collection.immutable.Map) BuildResObjMap;return targetScalaMap;}
另外一种:
public static scala.collection.immutable.Map<String, String> javaMapConvertToScalaMap(Map<String, String> map) {scala.collection.mutable.Map<String, String> scalaMap = JavaConverters.mapAsScalaMapConverter(map).asScala();Object objMap2 = Map$.MODULE$.<String, String>newBuilder().$plus$plus$eq(scalaMap.toIndexedSeq());Object BuildResObjMap = ((scala.collection.mutable.Builder) objMap).result();scala.collection.immutable.Map<String, String> targetScalaMap = (scala.collection.immutable.Map) BuildResObjMap;return targetScalaMap;}
最后终于能通过编译和运行, 终于开始开始调试spark了, nnd