I have a list of strings
val list = List("a", "b", "c", "d", "e")
and I want to have a map with keys as indexes of items in list. So I did the following:
def mapByIndexes(list: List[String]): Map[Int, String] = (1 to list.size).zip(list).toMap
However, the resulting map does not preserve the index order and I'm getting this as a result:
Map(5 -> "e", 1 -> "a", 2 -> "b", 3 -> "c", 4 -> "d")
How can I modify the code above so I'm getting a map with the following, natural order?
Map(1 -> "a", 2 -> "b", 3 -> "c", 4 -> "d", 5 -> "e")
Note: I'm aware that I can just sort the resulting map, but can I avoid that step and create a map that already preserves the order?
Edit: Solution with ListMap
described at Scala LinkedHashMap.toMap preserves order? works, but I don't like additional parentheses and _*
for so simple thing. Isn't there anything else so I can just have a chaining? If not, I will accept @pamu answer.
No, you can't. Sorting a
Map
doesn't make sense. But there areMap
implementations which store keys in natural order, such asTreeMap
(IntMap
also does, IIRC). Note that it is not the same as preserving insertion order, asListMap
andLinkedHashMap
do.No (at least, I don't think so), but you can easily define it:
Be aware that
ListMap
is basically a list (as the name says), so lookups in it are slower than any reasonable map implementation.Of course, you can do exactly the same with
TreeMap
.