网上代码 把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;
}
另外一种:文章来源:https://www.toymoban.com/news/detail-508436.html
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文章来源地址https://www.toymoban.com/news/detail-508436.html
到了这里,关于Java调用scala中map转换问题处理的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!