title: How to increment value of a given key with only one map lookup?
body: <p>Say I have a map:</p>
  
  <pre><code> var inventory = mutableMapOf("apples" to 1, "oranges" to 2)
  </code></pre>
  
  <p>and I want to increment the number of apples by one.</p>
  
  <p>However this does not work:</p>
  
  <pre><code>inventory["apples"]!!++ // Error:(9, 4) Variable expected
  </code></pre>
  
  <p>This neither:</p>
  
  <pre><code>var apples = inventory["apples"]!!
  ++apples
  println(inventory["apples"]) // prints "1" =&gt; the value was not incremented.
  </code></pre>
  
  <p>Which is surprising, since in Kotlin, numbers are <a href="https://kotlinlang.org/docs/reference/basic-types.html" rel="nofollow noreferrer">boxed when they are stored in instances of generic objects</a>. I would not expect that a copy is being made.</p>
  
  <p>It seems that the only way is to do something like:</p>
  
  <pre><code>var apples = inventory["apples"]!!
  ++apples
  inventory["apples"] = apples
  println(inventory["apples"]) 
  </code></pre>
  
  <p>Which both uses two lookups in the map and is extremely ugly and lengthy.</p>
  
  <p>Is there a way to increment a value of a given key using only one lookup?</p>
  
  <p>Also, could someone explain why the first two methods do not work?</p>