title: Scala Spark not returning value outside loop
body: <p>I'm using <code>Apache Spark</code> to extract the top 10 movies and genres from <code>movie lens</code> dataset. I was able to extract the ratings but the genres for each movies are in this format <code>Action|War|Crime</code>. I split the string using <code>|</code> and try to insert it inside <code>ArrayBuffer</code> or <code>ListBuffer</code>. It works fine inside the loop but as i tried to extract these outside the loop i get empty result. </p>
  
  <pre><code>    val rating_data = spark.read.format("csv").option("header", "true").load("data/ratings.csv")
  
  //read movie name file
  val movie_name = spark.read.format("csv").option("header", "true").load("data/movies.csv")
  
  //query for movie with rating 5, matching movie id in both csv files and extract the top 10 movies with highest count of 5
   val movie_id = rating_data.filter(rating_data("rating").===(5)).groupBy("movieId").count().orderBy(org.apache.spark.sql.functions.col("count").desc).take(10)
  
  var genre_list = Array[String]()
   movie_id.foreach(a =&gt; {
   #match the movies id in ratings and movie file
   val mv = movie_name.filter(movie_name("movieId").===(a(0)))
  
   mv.foreach(b =&gt; { 
  
   genre_list = b(2).toString.split('|').map(_.trim)
      genre_list.foreach(g =&gt; {
      mylist += g
      #mylist not empty , has elements 
  
        })
  })
  })
      println(mylist)  #empty
  }
  </code></pre>