Is the take
function safe to use on a Scala Map
? I thought that Map
s were unordered, so that myMap.take(2)
would return 2 random elements of myMap
. But testing on the interpreter makes me feel that it is safe:
scala> val z = Map(1 -> 10, 2 -> 20, 3 -> 30, 4 -> 40)
z: scala.collection.immutable.Map[Int,Int] = Map(1 -> 10, 2 -> 20, 3 -> 30, 4 -> 40)
scala> z.take(2)
res1: scala.collection.immutable.Map[Int,Int] = Map(1 -> 10, 2 -> 20)
scala> z.take(2)
res2: scala.collection.immutable.Map[Int,Int] = Map(1 -> 10, 2 -> 20)
scala> z.take(2)
res3: scala.collection.immutable.Map[Int,Int] = Map(1 -> 10, 2 -> 20)
scala> z.take(2)
res4: scala.collection.immutable.Map[Int,Int] = Map(1 -> 10, 2 -> 20)
So, is it safe to use take
on a Map
, or do I need to use a ListMap
?
You'll very likely get the same 2 every time you
take(2)
, but there's no telling which 2 you'll get in the first place.