5

If we use map then no need to import immutable map

scala> val map=Map[String,Int]()
map: scala.collection.immutable.Map[String,Int] = Map()

But if we use HashMap, then without doing import, it gives error.

scala> val a=HashMap[Int,Int]()
 <console>:7: error: not found: value HashMap
       val a=HashMap[Int,Int]()
             ^

but doing import scala.collection.immutable.HashMap, it works.

I also see it with Set and Hashset..

I notice one thing that Map and Set are trait and HashSet,HashMap are classes.

So Why it is so ???

EDIT

Class Stack and Queue is also exist in scala.collection package. then why do we need to import these classes. ???

The Archetypal Paul
  • 41,321
  • 20
  • 104
  • 134
Rishi
  • 1,279
  • 3
  • 20
  • 37
  • ```val map = Map()``` would do the same thing, since Map is immutable. and ```> map + ("Ahoy" -> 0) ``` works, thanks to the covariance. – jwinandy Feb 12 '13 at 16:58
  • As for Stack and Queue, I think they just a lot less used than List, Set and Map. – om-nom-nom Feb 12 '13 at 17:13
  • I am not sure but I think,these are the classes like HashMap. therefore they are not auto imported as your answer "Program to an interface, not an implementation" suggests. – Rishi Feb 12 '13 at 17:15
  • I just rolled back the edit because it completely changed the wording of the OP (and did not improve things, IMO), rather more than "missing tag added, paragraphs created". – The Archetypal Paul Jan 05 '17 at 13:15

4 Answers4

14

Program to an interface, not an implementation. Scala designers encouraged this by providing shortcuts to interfaces in Predef.

Community
  • 1
  • 1
om-nom-nom
  • 62,329
  • 13
  • 183
  • 228
3

It's because Predef is implicitly imported. Among others, it contains factory methods for common traits like Map. HashMap is a a concrete implementation, so if you used its factory method with type inference, you would tightly couple your declaration to an implementation.

thSoft
  • 21,755
  • 5
  • 88
  • 103
1

In Scala, it's idiomatic to prefer immutable types, so those are made available by default. If you want mutation you have to explicitly say so.

Another potential reason (and I'm just guessing) is that HashMap is a Map, and in many cases you don't really care what concrete implementation of Map you get, you just want something that can associate keys with values and have reasonably fast lookups. So it's more abstract to just say Map("foo" -> "bar") than HasArrayMappedTrie("foo" -> "bar").

overthink
  • 23,985
  • 4
  • 69
  • 69
  • But if we will try to use immutable queue without importing scala.collection.immutable.Queue, then it doesnt work. while both Map and Queue are in scala.collection package. Why is it so ?? – Rishi Feb 12 '13 at 16:59
  • 1
    As thSoft says below, it is because `Predef` is auomatically imported in all Scala apps. See [this line](https://github.com/scala/scala/blob/v2.10.0/src/library/scala/Predef.scala#L97) for where Map is made available. – overthink Feb 12 '13 at 17:17
  • Altough it is important to note that immutable types are preferred over mutable ones, there is actually an immutable HashMap implementation, so your second reason might be more significant. – thSoft Feb 13 '13 at 01:00
0

It probably has to do with the hierarchy of this collection class:

enter image description here

thikonom
  • 4,219
  • 3
  • 26
  • 30