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

问题描述

Java API返回一个 java.util.Map< java.lang.String,java.lang.Boolean> ;.我想把它放入一个 Map [String,Boolean]

A Java API returns a java.util.Map<java.lang.String,java.lang.Boolean>;. I would like to put that into a Map[String,Boolean]

所以想象我们有:

var scalaMap : Map[String,Boolean] = Map.empty
val javaMap = new JavaClass().map()   // Returns java.util.Map<java.lang.String,java.lang.Boolean>

你不能做 Map.empty ++ javaMap ,因为++方法不知道Java地图。我试过:

You can't do Map.empty ++ javaMap, because the ++ method does not know about Java maps. I tried:

scalaMap = Map.empty ++ new collection.jcl.MapWrapper[String,Boolean] {
    override def underlying = javaMap
}

和:

scalaMap = Map.empty ++ new collection.jcl.MapWrapper[java.lang.String,java.lang.Boolean] {
    override def underlying = javaMap
  }

这些都无法编译,因为泛型 - java.lang.String 与scala String不一样。

These both fail to compile, because of the generics - java.lang.String is not the same as a scala String.

有没有一个很好的方式来做,没有复制手动地图?

Is there a good way of doing this, short of copying the map manually?

编辑:谢谢,所有好的答案,我从他们中学到了很多。但是,我发现一个比我实际拥有的更简单的问题,我犯了一个错误。所以,如果你允许我,我会推广这个问题 - API实际返回的是

Thanks, all good answers, I learned a lot from all of them. However, I made a mistake by posting a simpler problem here than the one I actually have. So, if you allow me, I'll generalise the question - What the API actually returns is

java.util.Map<java.lang.String, java.util.Map<SomeJavaEnum,java.lang.String>>

我需要将其移动到Map [String,Map [SomeJavaEnum,String]]

And I need to move this to Map[String, Map[SomeJavaEnum,String]]

这似乎并不是太复杂,但它增加了一个额外的类型擦除,我发现将其移动到Scala地图的唯一方法是深入,复制它(使用你以下建议的一些技巧)。有任何提示吗?我通过为我的确切类型定义一个隐式转换来解决我的问题,所以至少这个丑陋被隐藏在自己的特质中,但仍然觉得有点笨拙的深层复制。

It probably does not seem like too much of a complication, but it adds an extra level of type erasure, and the only way I found of moving this to a Scala map was deep-copying it (using some of the techniques you suggested below). Anyone any hints? I kind of solved my problem by defining an implicit conversion for my exact types, so at least the ugliness is hidden in its own trait, but still feels a bit clumsy deep copying the lot.

推荐答案

至少与Scala 2.9.2有一个更简单的方法,集合转换:importimport collection.JavaConversions._并使用toMap。

At least with Scala 2.9.2 there's an easier way with the collections conversions: import "import collection.JavaConversions._" and use "toMap".

示例:

// show with Java Map:

scala> import java.util.{Map=>JMap}
scala> val jenv: JMap[String,String] = System.getenv()
jenv: java.util.Map[String,String] = {TERM=xterm, ANT_OPTS=-Xmx512m ...}

scala> jenv.keySet()
res1: java.util.Set[String] = [TERM, ANT_OPTS...]

// Now with Scala Map:

scala> import collection.JavaConversions._
scala> val env: Map[String,String] = System.getenv.toMap // <--- TADA <---
env: Map[String,String] = Map(ANT_OPTS -> -Xmx512m, TERM -> xterm ...)

// Just to prove it's got Scala functionality:

scala> env.filterKeys(_.indexOf("TERM")>=0)
res6: scala.collection.immutable.Map[String,String] = Map(TERM -> xterm,
  TERM_PROGRAM -> iTerm.app, ITERM_PROFILE -> Default)

它可以与java.util一起工作。 String到Boolean的映射。

It works fine with a java.util.map of String to Boolean.

这篇关于如何从java.util.Map转换为Scala Map的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 02:32