How do I create a List by applying an operation to all elements of a scala Map? -
I have a Scala map For everybody (K, V) MyMap should be Tuple (v.someMember, k) as an element in MyList Understanding the result is a new map for use For example: Or, if you want Do not assign more memory to more intermediate list, you can list with myMap and I have to make a list
MyList Combination is as follows:
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)}
toList and then
Map .
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))
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
Post a Comment