mapPartitions returns empty array <p>I have the following RDD which has 4 partitions:-</p> <pre><code>val rdd=sc.parallelize(1 to 20,4) </code></pre> <p>Now I try to call mapPartitions on this:-</p> <pre><code>scala&gt; rdd.mapPartitions(x=&gt; { println(x.size); x }).collect 5 5 5 5 res98: Array[Int] = Array() </code></pre> <p>Why does it return empty array? The anonymoys function is simply returning the same iterator it received, then how is it returning empty array? The interesting part is that if I remove println statement, it indeed returns non empty array:-</p> <pre><code>scala&gt; rdd.mapPartitions(x=&gt; { x }).collect res101: Array[Int] = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20) </code></pre> <p>This I don't understand. How come the presence of println (which is simply printing size of iterator) affecting the final outcome of the function?</p>