Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala HashMap: iterate in insertion order?

Tags:

hashmap

scala

I have been looking around, but most of them point to a java TreeMap. The only issue with that is I do not want to convert any Scala into java and back. If there really is no way, then I am ok with that, but I would like to hear it from some professionals just to be 100% sure and to have this question on here for others in the future to stumble upon when they have a similar issue. Thanks in advance.

EDIT:

Type: scala.collection.mutable.HashMap[String, String]

like image 952
Andy Avatar asked Apr 11 '12 20:04

Andy


1 Answers

In general, a Scala HashMap does not guarantee the original order.

However, there is the LinkedHashMap, which states: "The iterator and all traversal methods of this class visit elements in the order they were inserted."

What is the exact type you are dealing with? If you can decide which implementation to use, then you can choose one that maintains order. If you are just given something of type HashMap, then you're out of luck.

like image 118
dhg Avatar answered Nov 07 '22 18:11

dhg