title: MapReduce example in Scala
body: <p>I have this problem in Scala for a Homework.
  The idea I have had but have not been able to successfully implement is</p>
  
  <p>Iterate through each word, if the word is basketball, take the next word and add it to a map. Reduce by key, and sort from highest to lowest.</p>
  
  <p>Unfortunately I do not know how to take the next next word in a list of words.
  For example, i would like to do something like this:</p>
  
  <pre><code>val lines = spark.textFile("basketball_words_only.txt") // process lines in file
  
  // split into individual words
  val words = lines.flatMap(line =&gt; line.split(" "))
  
  var listBuff = new ListBuffer[String]() // a list Buffer to hold each following word
  
  val it = Iterator(words)  
  
  while (it.hasNext) {
    listBuff += it.next().next() // &lt;-- this is what I would like to do    
  }
  
  val follows = listBuff.map(word =&gt; (word, 1))
  val count = follows.reduceByKey((x, y) =&gt; x + y) // another issue as I cannot reduceByKey with a listBuffer
  
  val sort = count.sortBy(_._2,false,1)
  
  val result2 = sort.collect()
  
  for (i &lt;- 0 to result2.length - 1) {
   printf("%s follows %d times\n", result1(2)._1, result2(i)._2);
  }
  </code></pre>
  
  <p>Any help would be appreciated</p>