Scala Map vs HashMap

2020-03-08 08:58发布

问题:

Is there a difference between a Scala Map and a HashMap? I am using the scala.collection.immutable.HashMap.

回答1:

scala.collection.immutable.Map is the interface for immutable maps while scala.collection.immutable.HashMap is a concrete implementation.

Creating with Map() or Map.empty gives a special empty singleton map, with Map(a -> b) with up to 4 pairs yields specialized classes for such small maps, and 5 and upwards gives you scala.collection.immutable.HashMap