将Java Map转换为Scala Map [英] Convert Java Map to Scala Map

查看:235
本文介绍了将Java Map转换为Scala Map的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个java地图: java.util.Map< SomeObject,java.util.Collection< OtherObject>>
我要转换它到scala地图: Map [SomeObject,Set [OtherObject]]

I have a java map: java.util.Map<SomeObject, java.util.Collection<OtherObject>> and I would like to convert it to the scala map: Map[SomeObject, Set[OtherObject]]

我用过 mapAsScalaMap 但结果不是我想要的,结果是: Map [SomeObject,java.util.Collection [OtherObject]] 。如何修复它以将集合转换为集合?

I have used mapAsScalaMap but the result is not quite what I want, the result is: Map[SomeObject, java.util.Collection[OtherObject]]. How can I fix it to also convert the collection to a set?

注意:实际上我的原始问题是转换google的 ArrayListMultimap< SomeObject,OtherObject> ; MultiMap [SomeObject,OtherObject] ,但由于这是不可能的,我已经解决了问题。如果您有原始问题的解决方案,我也会接受它作为答案。

NOTE: actually my original problem was to convert google's ArrayListMultimap<SomeObject, OtherObject> to a MultiMap[SomeObject, OtherObject] but since this was not possible I've split the problem. If you have a solution for the original problem, I'll also accept it as the answer.

推荐答案

编辑:推荐的方法是使用 JavaConverters .asScala 方法:

Edit: the recommended way is now to use JavaConverters and the .asScala method:

import scala.collection.JavaConverters._
val myScalaMap = myJavaMap.asScala.mapValues(_.asScala.toSet)

这样做的好处是不使用神奇的隐式转换,而是显式调用 .asScala ,同时保持清洁和明智。

This has the advantage of not using magical implicit conversions but explicit calls to .asScala, while staying clean and consise.

原始答案 JavaConversions

您可以隐式使用 scala.collection.JavaConversions 在Java和Scala之间转换:

You can use scala.collection.JavaConversions to implicitly convert between Java and Scala:

import scala.collection.JavaConversions._
val myScalaMap = myJavaMap.mapValues(_.toSet)

调用 mapValues 将触发一个意蕴它从java Map 转换为scala Map ,然后调用 toSet 在java集合上隐式转换为scala集合,然后转换为 Set

Calling mapValues will trigger an implicit conversion from the java Map to a scala Map, and then calling toSet on the java collection with implicitly convert it to a scala collection and then to a Set.

默认情况下,它返回一个可变的 Map ,你可以得到一个带有 .toMap 的不可变的。

By default, it returns a mutable Map, you can get an immutable one with an additional .toMap.

Short-ish示例:

Short-ish example:

scala> val a: java.util.Map[String, java.util.Collection[String]] = new java.util.HashMap[String, java.util.Collection[String]]
a: java.util.Map[String,java.util.Collection[String]] = {}

scala> val b = new java.util.ArrayList[String]
b: java.util.ArrayList[String] = []

scala> b.add("hi")
res5: Boolean = true

scala> a.put("a", b)
res6: java.util.Collection[String] = []

scala> import scala.collection.JavaConversions._
import scala.collection.JavaConversions._

scala> val c = a.mapValues(_.toSet)
c: scala.collection.Map[String,scala.collection.immutable.Set[String]] = Map(a -> Set(hi))

scala> c.toMap
res7: scala.collection.immutable.Map[String,scala.collection.immutable.Set[String]] = Map(a -> Set(hi))

这篇关于将Java Map转换为Scala Map的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆