title: Spark Mapvalues vs. Map
body: <p>So I saw this question on stackoverflow asked by another user and I tried to write the code myself as I am trying to practice scala and spark:</p>
  
  <p>The question was to find the per-key average from a list:</p>
  
  <p>Assuming the list is: <code>( (1,1), (1,3), (2,4), (2,3), (3,1) )</code></p>
  
  <p>The code was:</p>
  
  <pre><code>val result = input.combineByKey(
  (v) =&gt; (v, 1),
  (acc: (Int, Int), v) =&gt; (acc._1 + v, acc._2 + 1),
  (acc1: (Int, Int), acc2: (Int, Int)) =&gt; (acc1._1 + acc2._1, acc1._2 + acc2._2)).
  map{ case (key, value) =&gt; (key, value._1 / value._2.toFloat) }  
  result.collectAsMap().map(println(_))
  </code></pre>
  
  <p>So basically the above code will create an RDD of type <code>[Int, (Int, Int)]</code> where the first <code>Int</code> is the key and the value is <code>(Int, Int)</code> where the first <code>Int</code> here is the addition of all the values with the same key and the second <code>Int</code> is the amount of times the key appeared.</p>
  
  <p><strong>I understand what is going on but for some reason when I rewrite the code like this:</strong></p>
  
  <pre><code>val result = input.combineByKey(
  (v) =&gt; (v, 1),
  (acc: (Int, Int), v) =&gt; (acc._1 + v, acc._2 + 1),
  (acc1: (Int, Int), acc2: (Int, Int)) =&gt; (acc1._1 + acc2._1, acc1._2 + acc2._2)).
  mapValues(value: (Int, Int) =&gt; (value._1 / value._2))
  result.collectAsMap().map(println(_))
  </code></pre>
  
  <p>When I use mapValues instead of map with the <code>case</code> keyword the code doesn't work.It gives an error saying <code>error: not found: type /</code> What is the difference when using map with case and mapValues. Since I thought map values will just take the value (which in this case is a (Int,Int)) and return to you a new value and the key remains the same for the key value pair. </p>