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
This should do it :
val listMap = ListMap(list : _*)
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