I am using a using a TrieMap on which I perform a .mapValues() operation which returns a MapLike[] object.But I need to convert it into HashMap, or any Serializable Map that maintains sort order, because it have to transfer over the network. What is the most performance efficient way to do this?
1 Answer
Using .mapValues
doesn't actually create a new TrieMap
, it just returns a view of the original. To actually transform and create a new TrieMap
out of your old collection, you could use the .transform
method with a slightly different signature, which returns a new collection of the same type. The only drawback is that you have to use the same value type:
scala> val map = TrieMap(1->5).transform((k, v) => v + 1)
map: scala.collection.concurrent.TrieMap[Int,Int] = TrieMap(1 -> 6)
scala> :t map
scala.collection.concurrent.TrieMap[Int,Int]
yourTrieMap.toMap
?.toSeq
and see if it can meet your needs? Perhaps SortedMap if necessary?