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?
If your comparator is on the insertion order, the tree will preserve the insertion order.
Lists. Lists preserve order, can contain duplicates, and are immutable.
An immutable map whose key-value pairs are sorted according to an scala. math. Ordering on the keys. Allows for range queries to be performed on its keys, and implementations must guarantee that traversal happens in sorted order, according to the map's scala.
A TreeMap is a Map that maintains its entries in ascending order, sorted according to the keys' natural ordering, or according to a Comparator provided at the time of the TreeMap constructor argument. The TreeMap class is efficient for traversing the keys in a sorted order.
From the LinkedHashMap
Scaladoc page:
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