1

I have defined a mutable map of maps

import scala.collection.mutable.Map

val default = Map.empty[String, Int].withDefaultValue(0)
val count = Map.empty[Any, Map[String, Int]].withDefaultValue(default)

which I populate/update as in

count("furniture")("table") += 1
count("furniture")("chair") = 6
count("appliance")("dishwasher") = 1

How can I iterate over all items in count? And why does count.keys return an empty Set()?

1

2 Answers 2

1

With default, does not create new Map when no value exists in collection, it just returns default value on such requests, and other changes are done on this default value.

count("furniture")("table") += 1
count("furniture")("chair") = 6
count("appliance")("dishwasher") = 1

count("banana") // will return Map with "table", "chair" & "dishwasher"

is equivalent

default("table") += 1
default("chair") = 6
default("dishwasher") = 1

And since you return this default value on any key, this default map will be returned on every call.

Your code will work like this.

count("furniture") = Map.empty[String, Int].withDefaultValue(0)
count("appliance") = Map.empty[String, Int].withDefaultValue(0)

count("furniture")("table") += 1
count("furniture")("chair") = 6
count("appliance")("dishwasher") = 1
Sign up to request clarification or add additional context in comments.

4 Comments

So would you know how a map that is instantiated with a new copy of the default map for any key can be created?
Also, why does count.keys return an empty Set(), while count("furniture").keys returns Set(chair, table) with the code I've posted in my question above?
In my code, key set returns list of all keys, and it works. In your case there are no keys, but a default value, which you modify, and count(Any) for anything will return this original default value, which you modified
You're right, count("furniture") returns the updated default map with all its entries. Would you think it would make sense to just check for the existence of a key before updating/inserting entries, and insert a copy (default.clone()) for any non-existing key?
0

There are several problems with your approach:

Issue #1:

val default = Map.empty[String,Int].withDefaultValue(0)

defines a value default. There is only one instance of this value and it can not be changed, since you defined as a val.

That means that your count map has a default value which is always the same instance of an empty map. Since count is empty, count("furniture") or count("appliance") is exactly the same as just default.

Issue #2:

withDefaultValue does not add entries to a map it just returns a default for undefined keys.

See @mavarazys answer

3 Comments

I don't think that «count("furniture") or count("applicance") is exactly the same as just default». The code in my question above actually returns Map(chair -> 6, table -> 1) for count("furniture")...
no it doesn't, i executed it and it returns Map(dishwasher -> 1, chair -> 6, table -> 1)
You're totally right, sorry for confusing things – and thanks for your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.