Why does a lookup in Map in this particular code take such a surprisingly long time?

Viewed 123

I have the following data structures:

val set: scala.collection.immutable.Set[String] = ...
val test1: scala.collection.immutable.Map[String,scala.collection.immutable.Set[String]] = ...
val test2: Array[scala.collection.immutable.Set[String]] = ...

set contains about 60,000 entires. test1 has two entries ("one" and "two") and each is a Set of Strings similar to set. test2 is similar to test1 but the keys are 0 and 1 instead.

Running test1.get("one").get.contains("somestring") takes a long time (about 1 second) but running test2(0).contains("somestring") is very fast.

I don't quite understand why there is such a big difference. Any ideas?

3 Answers
Related