1

I have a datatype of the following format:

Iterable[scala.collection.SortedMap[String,Double]]

and I need the following datatype:

scala.collection.mutable.Map[String, Double]

Thanks

user3370773
  • 435
  • 6
  • 11

3 Answers3

2

Convert the iterator first to a sequence, which can then be converted to a mutable Map. To illustrate this, let for instance

val a: scala.collection.SortedMap[String,Double] = SortedMap("a" -> 1.1, "b" -> 2.2)

Then

val m = scala.collection.mutable.Map( a.iterator.toSeq: _*)
m: scala.collection.mutable.Map[String,Double] = Map(b -> 2.2, a -> 1.1)

Note the ordering is lost in the resulting Map.

elm
  • 20,117
  • 14
  • 67
  • 113
  • Thanks for the reply, however using your syntax i get the following error: Error:(57, 95) type mismatch; found : Seq[scala.collection.SortedMap[String,Double]] required: Seq[(?, ?)] – user3370773 Nov 11 '14 at 10:24
  • It's about the type `Seq[...SortedMap]` in the answer, used `.iterator` and then `.toSeq`. – elm Nov 11 '14 at 10:51
  • I used the exact syntax your wrote including the .iterator. I get the comment posted above – user3370773 Nov 11 '14 at 10:58
2

Assuming that what you want is to transform scala.collection.SortedMap[String,Double] into a scala.collection.mutable.Map[String, Double], you can do something like:

val m1 = SortedMap("key1" -> 1.0, "key2" -> 2.0) // immutable
val m2 = scala.collection.mutable.Map[String, Double]() ++= m1 // mutable

But if you REALLY want to transform Iterable[scala.collection.SortedMap[String,Double]] into scala.collection.mutable.Map[String, Double], then it is a bit unclear what do you expect as result exactly.

Hope it helped!

EDIT: then it means you really want to transform the Iterable into a single mutable map. Well, you can do this, although it looks as a terribly bizarre conversion. It would be nice to know why you want to convert that:

val m1 = SortedMap("key1" -> 1.0, "key2" -> 2.0) // immutable
val m2 = SortedMap("key3" -> 3.0, "key4" -> 4.0) // immutable
val it = Iterable(m1, m2)

val z = scala.collection.mutable.Map[String, Double]() ++= it.foldLeft[List[(String, Double)]](Nil)((x,m) => m.toList ::: x)
ale64bit
  • 6,232
  • 3
  • 24
  • 44
  • thanks for the reply, but it still doesn't work. I get the following error: Error:(58, 54) type mismatch; found : Iterable[scala.collection.SortedMap[String,Double]] required: scala.collection.TraversableOnce[(String, Double)] – user3370773 Nov 11 '14 at 10:50
  • @user3370773 Then I guess you wanted to get rid of the Iterable. Check the edit of my answer and see if it works for you. – ale64bit Nov 11 '14 at 11:00
0

It looks more like a hack, but anyway

scala> val a = collection.SortedMap("a" -> 1.1, "b" -> 2.2)
a: scala.collection.SortedMap[String,Double] = Map(a -> 1.1, b -> 2.2)

scala> type MMap = collection.mutable.Map[String, Double]
defined type alias MMap

scala> a.to[({type l[A] = collection.mutable.Map[String, Double]})#l]
res1: scala.collection.mutable.Map[String,Double] = Map(b -> 2.2, a -> 1.1)

Convert Map to mutable.Map

scala> a.to[({type l[a] = MMap})#l]
res0: scala.collection.mutable.Map[String,Double] = Map(b -> 2.2, a -> 1.1)

Convert Iterable[Map] to mutable.Map

scala> val as = Iterable.fill(1)(a)
as: Iterable[scala.collection.SortedMap[String,Double]] = List(Map(a -> 1.1, b -> 2.2))

scala> val ms: MMap = as.flatMap(_.to[({type l[a] = MMap})#l])(collection.breakOut)
ms: MMap = Map(b -> 2.2, a -> 1.1)

Also note that in this case, intersecting keys will be replaced by later one. See this one on how to merge maps with the same keys.

Community
  • 1
  • 1
4e6
  • 10,696
  • 4
  • 52
  • 62