Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala Map implementation keeping entries in insertion order?

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?

like image 892
ebruchez Avatar asked Oct 01 '10 00:10

ebruchez


People also ask

Does scala map maintain insertion order?

If your comparator is on the insertion order, the tree will preserve the insertion order.

Does scala list preserve order?

Lists. Lists preserve order, can contain duplicates, and are immutable.

Are scala maps sorted?

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.

Does TreeMap maintain order?

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.


1 Answers

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."
like image 182
Randall Schulz Avatar answered Oct 10 '22 05:10

Randall Schulz