I have tried this but it does not work:
val map:Map[String,String] = for {
tuple2 <- someList
} yield tuple2._1 -> tuple2._2
How else would I convert a List of Tuple2s into a Map?
By default, Scala uses the immutable Map. If you want to use the mutable Map, you'll have to import scala. collection. mutable.
If you just want a mutable HashMap , you can just use x. toMap in 2.8 or collection. immutable. Map(x.
The concatenation of Scala map is obtained by utilizing ++ operator. Return Type: It returns a single map by concatenating two maps but it separates the identical keys. As we can see in above example, we joined two maps by using ++ operator.
A Map is an Iterable consisting of pairs of keys and values (also named mappings or associations). Scala's Predef object offers an implicit conversion that lets you write key -> value as an alternate syntax for the pair (key, value) .
It couldn't be simpler:
Map(listOf2Tuples: _*)
using the apply
method in Map
companion object.
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