scala – List – flatten

flatten method takes a list of lists and flattens it out to a single list:

It can only be applied to lists whose elements are all lists. Trying to flatten any other list will give a compilation error:

 

object List_Flatten extends App{

  val lst = List(List(1, 2), List(3), List(), List(4, 5)).flatten
println(lst)

  val ctry=List("america","india")
  val chr=ctry.map(_.toCharArray).flatten
  println(chr)
}


List(1, 2, 3, 4, 5)
List(a, m, e, r, i, c, a, i, n, d, i, a)

Leave a comment