Scala Map implementation keeping entries in insertion order?
Asked Answered
W

6

51

In Java, I use LinkedHashMap for this purpose. The documentation of Java's LinkedHashMap is very clear that it has "predictable iteration order" and I need the same in Scala.

Scala has ListMap and LinkedHashMap, but the documentation on what they do exactly is poor.

Question: Is Scala's LinkedHashMap or ListMap the implementation to use for this purpose? If not, what other options are available besides using the Java's LinkedHashMap directly?

Woodrum answered 1/10, 2010 at 0:30 Comment(2)
just to note, I don't recommend using scala's ListMap classes in general. They have linear time complexity for most operations. So unless your map is tiny, logic that uses listmaps will really drag. docs.scala-lang.org/overviews/collections/…Reformer
Unfortunately, because ListMap is only immutable, it's also hard to "find" like if you make it part of another nested type even for a val, it needs to be imported explicitly. It's not clear to me why it isn't already in scope like List and Map are.Stoup
S
54

From the LinkedHashMap Scaladoc page:

  • "This class implements mutable maps using a hashtable. The iterator and all traversal methods of this class visit elements in the order they were inserted."
Sisterly answered 1/10, 2010 at 2:45 Comment(0)
B
19

The difference between the two is that LinkedHashMap is mutable while ListMap is immutable. Otherwise they both are MapLike and also preserve insertion order.

Bjorn answered 25/10, 2013 at 2:22 Comment(2)
There are both immutable ListMap and mutable ListMap.Mariel
other difference is that ListMap has linear complexity in lookup and insertionCutlor
M
7

For LinkedHashMap, the answer is pretty clear that it preserves the order of insertion.

But for ListMap, it seems that there are some confuses here.

Firstly, there are two ListMap.

  • scala.collection.mutable.ListMap
  • scala.collection.immutable.ListMap.

Secondly, the document for ListMap has something wrong as far as I tried.

mutable.ListMap

The actual order is not the insertion order as it says.

And it is not the inverse order of insertion, either. The result I tried is [forth, second, first, third]

A simple mutable map backed by a list, so it preserves insertion order.

immutable.ListMap

As the document saying that, the order is the insertion order.

One thing to notice is that it is stored internally in reversed insertion order. And the internally stored order and the iterable/traversal order are two things. The internally stored order decides the time complexity of lookup methods such as head/last/tail/init/.

This class implements immutable maps using a list-based data structure. List map iterators and traversal methods visit key-value pairs in the order whey were first inserted.

Entries are stored internally in reversed insertion order, which means the newest key is at the head of the list.

Mariel answered 22/4, 2017 at 6:23 Comment(1)
Interesting. Did you report the conflict between real and documented behaviour to authors? github.com/scala/bug Edit: Seems someone did, and it's considered for deprecation in 2.13 github.com/scala/bug/issues/9893Wymore
O
4

ListMap doesn't preserves the order of insertion.

enter image description here

Only LinkedHashMap maintains the order of elements the way they are inserted.

enter image description here

If you want to maintain order in Lists otherthan Map you can use LinkedList

enter image description here

Olimpia answered 12/12, 2016 at 3:35 Comment(1)
you're evaluating mutable listmaps but most people use the immutable listmaps which is a completely different implementationReformer
W
4

Scala 2.13 is introducing two new immutable implementations of Map which keep insertion order: VectorMap and SeqMap. See this PR:

Currently there isn't any known immutable map which also maintains key insertion order while keeping effectively constant lookup time on key, so the only known implementations are done by combining a Vector with a HasMap (or in Scala's case HashMap/ChampHashMap)

As of writing, Scala 2.13 is still scheduled to be released in 2018.

UPDATE 2021-04-14: Scala 3.13 does have VectorMap now. SeqMap is just a "generic trait for ordered immutable maps". Besides the new VectorMap and the older ListMap, there is also a new TreeSeqMap.

Note that mutable.ListMap got deprecated with 2.13, but immutable.ListMap is still current.

Woodrum answered 27/8, 2018 at 17:16 Comment(0)
R
3
  • LinkedHashmap is in the order it was added
  • (immutable) ListMap is in the backward order it was added (i.e. the last one added is first)

LinkedHashmap is only implemented as a mutable map ListMaps are implemented in both the mutable and immutable packages, however only the immutable ListMaps maintain the backwards ordering. (mutable listmaps do not maintain order)

Reformer answered 17/3, 2016 at 22:50 Comment(5)
this is a nice article that breaks down the various map implementations in scala alvinalexander.com/scala/…Reformer
ListMap doesn't even maintain order in backward order as you are saying. It randomly displays from the list.Olimpia
Actually the document of scala api is wrong. And it is different between immutable.ListMap and mutable.ListMapMariel
@Olimpia you are referring to mutable listmaps only. I've updated my answer to differentiate between the 2 types (mutable and immutable).Reformer
just wanted to note that both ListMaps implementations have slow time complexity for most functions so it reduces their practicality in using with large amounts of data. Unless mutability is an absolute deal killer, you should use LinkedHashmapReformer

© 2022 - 2024 — McMap. All rights reserved.