Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way in scala to convert from any Map to java.util.Map?

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

like image 232
Michael Neale Avatar asked Jan 28 '09 06:01

Michael Neale


People also ask

Can we convert Map to set Java?

You can not convert Java Map to Java Set.

Is Scala Map a HashMap?

Scala HashMap is used to store objects and it take the object in the form of key value pair. For every value there should be one key associated with it. Scala collection contains this Hashmap and it is the implementation of MAP.

Which of the given methods is used to convert the Map into collection?

The stream() method returns a stream of the keys from the Set of the map keys returned by Map. keySet(). The collect() method of the Stream class collects the stream of keys in a List.

How do I convert a list to Map in Scala?

To convert a list into a map in Scala, we use the toMap method. We must remember that a map contains a pair of values, i.e., key-value pair, whereas a list contains only single values. So we have two ways to do so: Using the zipWithIndex method to add indices as the keys to the list.


1 Answers

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.

like image 80
Michael Neale Avatar answered Oct 12 '22 23:10

Michael Neale