FlatMap

FltatMap takes a function as an argument. The function given to flatMap() doesn't work on nested lists but it produces a new collection. It can be defined formally as follows:

def flatMap[B](f: (A) ⇒ GenTraversableOnce[B]): Traversable[B]  

Let's see an example as follows:

//Applying function on nested lists and then combining output back together
scala> List(List(2,4), List(6,8)) flatMap(x => x.map(x => x * x))
res4: List[Int] = List(4, 16, 36, 64)

We have just about finished covering the uses of Scala collection features. Also note that methods such as Fold(), Reduce(), Aggregate(), Collect(), Count(), Find(), and Zip() can be used to pass from one collection to another (for example, toVector, toSeq, toSet, toArray). However, we will see such examples in upcoming chapters. For the time being, it's time to see some performance characteristics of different Scala collection APIs.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.122.46