pacman
pacman

Reputation: 837

Scala SortedMap under the hood

I use SortedMap for storing ordered elements:

val map = SortedMap("Kim" -> 90, "Steve" -> 22, "Alex" -> 12)

My questions are:

  1. What is the implementation stand under SortedMap?
  2. Does SortedMap same to Red black tree?
  3. SortedMap in Java is an interface, why in Scala it can be instantiated?

Upvotes: 1

Views: 106

Answers (1)

Sleiman Jneidi
Sleiman Jneidi

Reputation: 23329

The default implantation of SortedMap in Scala is a TreeMap which is an immutable Red-Black tree. Not to be confused with Java's java.util.TreeMap which is mutable.

Why SortedMap in Scala can be instantiated? It can't, it's a trait but the SortedMap companion object gives you the flexibility to initialise the object this way.

Upvotes: 3

Related Questions