如何将 java Map 转换为 LinkedHashMap[String,ArrayList[String]] 类型的 Scala Map? [英] How to convert java Map to scala Map of type LinkedHashMap[String,ArrayList[String]]?

查看:129
本文介绍了如何将 java Map 转换为 LinkedHashMap[String,ArrayList[String]] 类型的 Scala Map?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从Java API我得到

from Java API I get

java.util.LinkedHashMap[String,java.util.ArrayList[String]]

然后我需要将其作为参数传递给 Scala 程序

which I then need to pass as a parameter to a scala program

val rmap = Foo.baz(parameter)

这个参数是scala类型的:

This parameter is of scala type:

Map[String,List[String]]

那么我怎样才能轻松转换

So how can I easily convert

java.util.LinkedHashMap[String,java.util.ArrayList[String]]

Map[String,List[String]]

我尝试使用 import scala.collection.JavaConversions._ 但在我的情况下这不起作用(或者至少我猜是这样)因为 scala 代码在模板函数中,我只能放置 import scala.collection.JavaConversions._ 函数内部.模板函数是这样的:

I tried using import scala.collection.JavaConversions._ but in my case this does not work (or at least thats what I guess) because the scala code is in template function and I can only place import scala.collection.JavaConversions._ inside the function. the template function is like:

def someFunc(param: java.util.LinkedHashMap[String,java.util.ArrayList[String]]) = {
import scala.collection.JavaConversions._
val rmap = Foo.baz(param) // param is of scala type Map[String,List[String]]
.......

现在scala不会自动将java类型转换为scala类型.

Now scala is not auto converting java type to scala type.

这是我得到的编译器错误:引发的错误是:类型不匹配;发现:java.util.LinkedHashMap[String,java.util.ArrayList[String]] 需要:Map[String,List[String]]

This is the compiler error I get: Error raised is : type mismatch; found : java.util.LinkedHashMap[String,java.util.ArrayList[String]] required: Map[String,List[String]]

推荐答案

你有两个问题:scala 中的 Map 和 List 与 java 版本不同.

You've got two problems: Map and List in scala are different from the java versions.

scala.collection.JavaConversions.mapAsScalaMap 创建一个可变映射,因此使用可变映射,以下工作:

scala.collection.JavaConversions.mapAsScalaMap creates a mutable map, so using the mutable map, the following works:

import scala.collection.JavaConversions._
import scala.collection.mutable.Map

val f = new java.util.LinkedHashMap[String, java.util.ArrayList[String]]
var g: Map[String, java.util.ArrayList[String]] = f

ArrayList 有点难.以下工作:

The ArrayList is a bit harder. The following works:

import scala.collection.JavaConversions._
import scala.collection.mutable.Buffer

val a = new java.util.ArrayList[String]
var b: Buffer[String] = a

但是如果我们尝试

import scala.collection.JavaConversions._
import scala.collection.mutable.Map
import scala.collection.mutable.Buffer

val f = new java.util.LinkedHashMap[String, java.util.ArrayList[String]]
var g: Map[String, Buffer[String]] = f

这不起作用.您最好的选择是为此自己定义显式隐式转换.

this doesn't work. Your best option is to define an explicit implicit conversion yourself for this.

这篇关于如何将 java Map 转换为 LinkedHashMap[String,ArrayList[String]] 类型的 Scala Map?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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