How do I create a List by applying an operation to all elements of a scala Map? -


I have a Scala map myMap and I have to make a list MyList Combination is as follows:

For everybody (K, V) MyMap should be Tuple (v.someMember, k) as an element in MyList

Understanding the result is a new map for map and . It's a better way to start with an empty list and add elements through the map (key, value) through pairs.

  var myList = List.empty [(double, string)] Mimap Foreach {case (k, v) = & gt; MyList :: = (v.someMember, k)}    

use toList and then Map .

For example:

  scala> Map ("a" -> 1, "b" - & gt; 2) .toList.map {case (k, v) = & gt; (K.size, v)} res12: list [(int, int)] = list ((1,1), (1,2))   

Or, if you want Do not assign more memory to more intermediate list, you can list with map with breakout

  import scala.collection.breakOut Can Scale & gt; Val L: List [(Int, Int)] = Map ("one" -> 1, "b" -> 2) .map ({case (k, v) => (k.size, v )}} (Breakout) L: list [(int, int) = list ((1,1), (1,2))    

Comments

Popular posts from this blog

java - ImportError: No module named py4j.java_gateway -

python - Receiving "KeyError" after decoding json result from url -

.net - Creating a new Queue Manager and Queue in Websphere MQ (using C#) -