This is what I usually write ... not sure if there are better solutions.
map.get(key) match { case None => map.put(key, defaultValue) case Some(v) => map(key) = updatedValue }
In fact, update and put same for mutable display, but I usually use update for existing records and put for new, just for readability.
Another thing is that if you can understand what the final value is without checking the existence of the key, you can simply write map(key) = value , while it automatically creates / replaces the record.
Finally, expressions such as map(key) += 1 actually work in Map (this is usually true for collections with the update function), as well as for many simple numerical operations. \
To resolve double-tagging, use a mutable object instead of immutable values:
class ValueStore(var value: ValueType) val map = new Map[KeyType, ValueStore] ... map.get(key) match { case None => map.put(key, new ValueStore(defaultValue)) case Some(v) => v.value = updatedValue }
As I mentioned in a comment, the basic structure of a HashMap is a HashTable , which actually uses this mutable shell class approach. HashMap is a more cool class, but sometimes you need to do duplicate calculations.
Kane
source share