flatMap is a Java and Scala function that works by applying a function that returns a sequence for each element in the list, and flattening the results into the original list.
flatMap works by applying a function that returns a sequence for each element in the list, and flattening the results into the original list. This is easier to show than to explain:
scala> def g(v:Int) = List(v-1, v, v+1)
g: (v: Int)List[Int]
scala> l.map(x => g(x))
res64: List[List[Int]] = List(List(0, 1, 2), List(1, 2, 3), List(2, 3, 4), List(3, 4, 5), List(4, 5, 6))
scala> l.flatMap(x => g(x))
res65: List[Int] = List(0, 1, 2, 1, 2, 3, 2, 3, 4, 3, 4, 5, 4, 5, 6)
Source: http://www.brunton-spall.co.uk/post/2011/12/02/map-map-and-flatmap-in-scala/