Scala 映射到 Java HashMap

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23350784/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 06:13:51  来源:igfitidea点击:

Scala Map to Java HashMap

scalascala-collectionsscala-java-interop

提问by gknauth

I have a Scala function f(s1: String, s2: String): Map[String,String]

我有一个 Scala 函数 f(s1: String, s2: String): Map[String,String]

I want to allow a colleague coding in Java to call a Java method I'm writing:

我想允许用 Java 编码的同事调用我正在编写的 Java 方法:

HashMap<String, String> f(String s1, String s2)

HashMap<String, String> f(String s1, String s2)

This calls my Scala function. What I've found, on the Java side, is that Scala has returned a scala.collection.immutable.Map.

这调用了我的 Scala 函数。我发现,在 Java 方面,Scala 返回了一个scala.collection.immutable.Map.

How do I make a Java HashMap out of it? Or should I be doing something else?

如何从中制作 Java HashMap?还是我应该做点别的?

回答by Justin Pihony

I think what you are looking for is here. Specifically the mapAsJavaMapmethod.

我想你要找的就在这里。具体mapAsJavaMap方法。

Where the preferred usage is explained in this SO questionUsing imported implicitsand asJava

在此 SO 问题中解释了首选用法使用导入implicitsasJava