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).
You can not convert Java Map to Java Set.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With