0

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?

4
  • yourTrieMap.toMap?
    – moem
    Commented Nov 7, 2015 at 17:43
  • 2
    TrieMap is Serializable, and HashMap doesn't maintains order. I don't really get your question Commented Nov 7, 2015 at 20:08
  • 1
    As maps don't maintain order, you could start with .toSeq and see if it can meet your needs? Perhaps SortedMap if necessary?
    – wwkudu
    Commented Nov 8, 2015 at 6:46
  • @kosii, my bad. Just edited the question to explain the use case further.
    – jaywalker
    Commented Nov 8, 2015 at 10:52

1 Answer 1

2

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]

Not the answer you're looking for? Browse other questions tagged or ask your own question.