This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Two questions about immutable maps

7 replies
Larry Yogman
Joined: 2009-05-11,
User offline. Last seen 42 years 45 weeks ago.

Using Scala 2.8 (2.8.0.r20327-b20091231020112), comparing immutable
collections to recently 1.0 Google collections framework

http://code.google.com/p/google-collections/

1) Why is the keySet of an immutable map not an immutable set?

scala> val m = Map(2->2, 1->1)
m: scala.collection.immutable.Map[Int,Int] = Map(2 -> 2, 1 -> 1)

scala> val s = m.keySet
s: scala.collection.Set[Int] = Set(2, 1)

scala> s.getClass
res0: java.lang.Class[_] = class scala.collection.MapLike$DefaultKeySet

2) Is the order of iteration - in the map and its keySet - guaranteed to be
the order of insertion? Scaladoc doesn't seem to say, but that is the
behavior I currently see:

scala> Map(1->1,2->2).keySet.mkString
res7: String = 12

scala> Map(2->2,1->1).keySet.mkString
res8: String = 21

ounos
Joined: 2008-12-29,
User offline. Last seen 3 years 44 weeks ago.
Re: Two questions about immutable maps

The keySet of an immutable Map is also immutable, yes.

Order of insertion is not maintained in a hashtable-based Map (just as
in java.util.HashMap, or google collections immutable Map). You could
use (mutable) LinkedHashMap for that.

Dimitrs

2010/1/14 Larry Yogman :
>
> Using Scala 2.8 (2.8.0.r20327-b20091231020112), comparing immutable
> collections to recently 1.0 Google collections framework
>
> http://code.google.com/p/google-collections/
>
> 1) Why is the keySet of an immutable map not an immutable set?
>
> scala> val m = Map(2->2, 1->1)
> m: scala.collection.immutable.Map[Int,Int] = Map(2 -> 2, 1 -> 1)
>
> scala> val s = m.keySet
> s: scala.collection.Set[Int] = Set(2, 1)
>
> scala> s.getClass
> res0: java.lang.Class[_] = class scala.collection.MapLike$DefaultKeySet
>
> 2) Is the order of iteration - in the map and its keySet - guaranteed to be
> the order of insertion?  Scaladoc doesn't seem to say, but that is the
> behavior I currently see:
>
> scala> Map(1->1,2->2).keySet.mkString
> res7: String = 12
>
> scala> Map(2->2,1->1).keySet.mkString
> res8: String = 21
> --
> View this message in context: http://old.nabble.com/Two-questions-about-immutable-maps-tp27166224p2716...
> Sent from the Scala - User mailing list archive at Nabble.com.
>
>

Larry Yogman
Joined: 2009-05-11,
User offline. Last seen 42 years 45 weeks ago.
Re: Two questions about immutable maps

The return type of immutable.Map.keySet is not immutable.Set - I was asking
if there is a good reason why

scala> Map(1->1).keySet.asInstanceOf[Set[Int]]
java.lang.ClassCastException: scala.collection.MapLike$DefaultKeySet cannot
be c
ast to scala.collection.immutable.Set
...

As for order of iteration, google collection javadoc does promise to
preserve insertion order -

http://google-collections.googlecode.com/svn/trunk/javadoc/index.html?ht...

A very useful feature, and I was asking if there's a good reason Scala 2.8
collections don't do the same thing.

Dimitris Andreou wrote:
>
> The keySet of an immutable Map is also immutable, yes.
>
> Order of insertion is not maintained in a hashtable-based Map (just as
> in java.util.HashMap, or google collections immutable Map). You could
> use (mutable) LinkedHashMap for that.
>
> Dimitrs
>
>

Chris Marshall
Joined: 2009-06-17,
User offline. Last seen 44 weeks 4 days ago.
RE: Two questions about immutable maps
> Date: Thu, 14 Jan 2010 11:07:40 -0800
> From: lyogman [at] pervasive [dot] com
> To: scala-user [at] listes [dot] epfl [dot] ch
> Subject: Re: [scala-user] Two questions about immutable maps
>
>
> The return type of immutable.Map.keySet is not immutable.Set - I was asking
> if there is a good reason why
>

The return type is collection.Set which happens to be immutable (because it has no mutators) but is not an instance of immutable.Set, which is another class entirely.


Do you have a story that started on Hotmail? Tell us now
Larry Yogman
Joined: 2009-05-11,
User offline. Last seen 42 years 45 weeks ago.
Re: Two questions about immutable maps

Better link

http://google-collections.googlecode.com/svn/trunk/javadoc/com/google/common/collect/ImmutableMap.html#keySet()
http://google-collections.googlecode.com/svn/trunk/javadoc/com/google/common/collect/ImmutableMap.html#keySet()

Randall R Schulz
Joined: 2008-12-16,
User offline. Last seen 1 year 29 weeks ago.
Re: Two questions about immutable maps

On Thursday January 14 2010, Larry Yogman wrote:
> The return type of immutable.Map.keySet is not immutable.Set - I was
> asking if there is a good reason why

Mutability is not a nominal characteristic, though the classes that are
mutable are organized separately from those that are. Mutability is a
property that is entailed by the presence of mutating methods.

If you look at the trait collection.Set, you'll see that there are no
mutating methods in that trait. Furthermore, you'll see that
implementations of Set in both collection.mutable and
collection.immutable are derived from this shared (and immutable)
common trait collection.Set.

> ...

Randall Schulz

ounos
Joined: 2008-12-29,
User offline. Last seen 3 years 44 weeks ago.
Re: Two questions about immutable maps

2010/1/14 Larry Yogman :
>
> The return type of immutable.Map.keySet is not immutable.Set - I was asking
> if there is a good reason why
>
> scala> Map(1->1).keySet.asInstanceOf[Set[Int]]
> java.lang.ClassCastException: scala.collection.MapLike$DefaultKeySet cannot
> be c
> ast to scala.collection.immutable.Set
> ...
>
> As for order of iteration, google collection javadoc does promise to
> preserve insertion order -
>
> http://google-collections.googlecode.com/svn/trunk/javadoc/index.html?ht...

Oh, thanks, I missed that. Now I realize what Kevin was saying the
other day, that they didn't pay too much attention on memory footprint
of ImmutableMap.

>
> A very useful feature, and I was asking if there's a good reason Scala 2.8
> collections don't do the same thing.
>
>
> Dimitris Andreou wrote:
>>
>> The keySet of an immutable Map is also immutable, yes.
>>
>> Order of insertion is not maintained in a hashtable-based Map (just as
>> in java.util.HashMap, or google collections immutable Map). You could
>> use (mutable) LinkedHashMap for that.
>>
>> Dimitrs
>>
>>
> --
> View this message in context: http://old.nabble.com/Two-questions-about-immutable-maps-tp27166224p2716...
> Sent from the Scala - User mailing list archive at Nabble.com.
>
>

Larry Yogman
Joined: 2009-05-11,
User offline. Last seen 42 years 45 weeks ago.
RE: Two questions about immutable maps

christopher marshall-2 wrote:
>
>
> The return type is collection.Set which happens to be immutable (because
> it has no mutators) but is not an instance of immutable.Set, which is
> another class entirely.
>
>

The type returned does not express the guarantee that the set is immutable -
so I can't pass this set to functions that require immutable args. After
all, collection.Set could be a read-only view of something mutable.

Google collections wins again (note return type)
http://google-collections.googlecode.com/svn/trunk/javadoc/com/google/common/collect/ImmutableMap.html#keySet()
http://google-collections.googlecode.com/svn/trunk/javadoc/com/google/common/collect/ImmutableMap.html#keySet()

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland