scala中有没有办法从任何Map转换为java.util.Map?

发布于 2024-07-13 00:36:41 字数 85 浏览 7 评论 0原文

我使用了很多 scala 映射,偶尔我想将它们作为映射传递给需要 java.util.Map 的遗留 java api(而且我不关心它是否会丢弃任何更改)。

I use a lot of scala maps, occasionally I want to pass them in as a map to a legacy java api which wants a java.util.Map (and I don't care if it throws away any changes).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

佞臣 2024-07-20 00:36:41

我发现一个优秀的库在这方面做得更好:

http://github.com/jorgeortiz85/scala -javautils

(坏名字,很棒的库)。 您可以根据您想要的方向显式调用 .asJava 或 .asScala。 没有什么惊喜。

An excellent library I have found that does a better job of this:

http://github.com/jorgeortiz85/scala-javautils

(bad name, awesome library). You explicitly invoke .asJava or .asScala depending on what direction you want to go. No surprises.

蓬勃野心 2024-07-20 00:36:41

Scala 为 Java 集合提供了包装器,以便它们可以用作 Scala 集合,但反之则不然。 话虽这么说,编写自己的包装器可能并不难,而且我确信它对社区很有用。 这个问题经常出现。

Scala provides wrappers for Java collections so that they can be used as Scala collections but not the other way around. That being said it probably wouldn't be hard to write your own wrapper and I'm sure it would be useful for the community. This question comes up on a regular basis.

回梦 2024-07-20 00:36:41

这个问答讨论了这个确切的问题和可能的解决方案。 它建议不要进行透明转换,因为它们可能会产生非常奇怪的副作用。 它提倡使用 scala-javautils 代替。 我已经在一个大型项目中使用它们几个月了,发现它们非常可靠且易于使用。

This question and answer discuss this exact problem and the possible solutions. It advises against transparent conversions as they can have very strange side-effects. It advocates using scala-javautils instead. I've been using them in a large project for a few months now and have found them to be very reliable and easy to use.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文