I have a list of type List(String,String) and I wanted to convert it to map. When I used toMap method I found that it does not preservers the order of data that is there in the List. However my goal is to convert the list to Map by keeping the order of the data same as of List. I learned that ListMap preserves the insertion order(but it is immutable) so I can use the LinkedHashMap with map function to insert the data sequentially into LinkedHashMap but that means I need to iterate over all the elements which is pain. Can anyone please suggest me a better approach?
Thanks
-
"my goal is to convert the list to Map by keeping the order of the data same as of List". Maps don't work like thatn. m. could be an AI– n. m. could be an AI2017-01-18 15:09:27 +00:00Commented Jan 18, 2017 at 15:09
-
" I learned that ListMap preserves the insertion order" How did you learn that? There's nothing in the documentation.n. m. could be an AI– n. m. could be an AI2017-01-18 15:18:19 +00:00Commented Jan 18, 2017 at 15:18
-
I got to know from this post stackoverflow.com/questions/3835743/…Explorer– Explorer2017-01-18 15:19:27 +00:00Commented Jan 18, 2017 at 15:19
-
Apparently the answers there tell you to use LinkedHashMap, why not give it a try.n. m. could be an AI– n. m. could be an AI2017-01-18 15:28:25 +00:00Commented Jan 18, 2017 at 15:28
-
@n.m. I am trying both and learning about the performance too of both the collections.Explorer– Explorer2017-01-18 15:36:52 +00:00Commented Jan 18, 2017 at 15:36
Add a comment
|
3 Answers
This should do it :
val listMap = ListMap(list : _*)
5 Comments
Explorer
Thanks @C4stor for your comment, it worked. I am new to scala and a small explanation on the annotation would help a lot in understanding in depth.
Kevin Meredith
ListMap#apply accepts tuple varargs. By calling
: _*, list's elements are applied in a varargs fashion, as I understand. Perhaps this gist will help. See, also, stackoverflow.com/questions/1008783/using-varargs-from-scala for more info.C4stor
@KevinMeredith got it right. I think a more relevant question to understand : _* is stackoverflow.com/questions/6051302/… which have, hopefully, all information you need :)
hartmut27
"colon underscore star" works nice, but my favorite is
mylist.to(ListMap) (Scala >=2.13) mentioned by @Jasper-MC4stor
Sure, but scala 2.13 was released more than 2 years after this question was asked, so there's that ^^
In Scala 2.13 or later:
scala> import scala.collection.immutable.ListMap
import scala.collection.immutable.ListMap
scala> val list = List((1,2), (3,4), (5,6), (7,8), (9,0))
list: List[(Int, Int)] = List((1,2), (3,4), (5,6), (7,8), (9,0))
scala> list.to(ListMap)
res3: scala.collection.immutable.ListMap[Int,Int] = ListMap(1 -> 2, 3 -> 4, 5 -> 6, 7 -> 8, 9 -> 0)
Comments
Don't use a ListMap. They are extremely imperformant. Since they are structured as lists they have linear lookup performance (https://docs.scala-lang.org/overviews/collections/performance-characteristics.html)
I'd advise instanciating a mutable LinkedHashmap and then assigning it to a val defined as a collections.Map. The collection.Map interface doesn't expose mutable methods so the map is immutable to any entity accessing it.