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

时间:2022-06-19 15:48:36

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).

我使用了很多scala贴图,偶尔我希望将它们作为映射传递给想要java.util.Map的遗留java api(我不在乎它是否抛弃任何更改)。

3 个解决方案

#1


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.

(坏名字,真棒图书馆)。您明确调用.asJava或.asScala,具体取决于您想要的方向。没有惊喜。

#2


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.

Scala为Java集合提供了包装器,因此它们可以用作Scala集合,但不能用作其他方式。可以说,编写自己的包装器并不难,我相信它对社区有用。这个问题定期出现。

#3


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.

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

#1


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.

(坏名字,真棒图书馆)。您明确调用.asJava或.asScala,具体取决于您想要的方向。没有惊喜。

#2


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.

Scala为Java集合提供了包装器,因此它们可以用作Scala集合,但不能用作其他方式。可以说,编写自己的包装器并不难,我相信它对社区有用。这个问题定期出现。

#3


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.

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