Scala list flatMap() operation with examples

 169 total views,  2 views today

Welcome to the Scala series. In this tutorial, you will learn about the Scala List flatMap() operation with examples.

Before learning flatMap() let us understand flattening?

What is flattening?

Flattening helps in converting a collection of several lists or collections into a single collection. It merges all list of collections to form a single collection.

Flattening =  Collection<Collection <T>> to Collection<T>

Examples

[[a,b,c],[d,e,f],[g,h],[i,j,k,l,m] = [a,b,c,d,e,f,g,h,i,j,k,l,m]

It flatten out several lists of data into single list.

What is Scala flatMap() ?

Scala flatMap() flattens the stream of collections to form a single stream.It uses map() + flatten() operations on the input stream.

It transforms the data as well as flatten the collection.

Scala flatMap() operation is applicable to all types of Scala collection.

flatMap() Syntax

def flatMap[B](f: (A) => IterableOnce[B]): List[B]

Builds a new list by applying a function to all elements of this list and using the elements of the resulting collections.

Scala List flatMap() examples

In this flatMap() example, we have created a list that has two list objects internally and called flatMap() method to convert/merge into a single list.

object FlatMap {

  def main(args: Array[String]): Unit = {

    //create a List
    var list = List(List(1, 2, 3), List(4, 5), List(6, 7, 8))

    //Apply flatMap
    list.flatMap(ele => ele).foreach(println)

  }
}

Output

1
2
3
4
5
6
7
8

In this flatMap() example, we have created a list that has strings and called flatMap() method to split elements and create a list.

object FlatMap {

  def main(args: Array[String]): Unit = {

    //create a List
    var list = List("apple", "hello", "London")

    //Apply flatMap
    list.flatMap(ele => ele.split("")).foreach(println)

  }
}

Output

a
p
p
l
e
h
e
l
l
o
L
o
n
d
o
n

ReferencesScala flatMap() operation

Would you like to learn more about Scala features, please check out Scala – a complete guide series. In this tutorial guide, you will find useful Scala functions that can be applied to the collection.

I hope you like this tutorial and you were able to understand the Scala list flatMap() operation with examples.

Thanks for reading and please give us a thumb up and comment below.

Tags :

About the Author

Rajasekar

Hey There, My name is Rajasekar and I am the author of this site. I hope you are liking my tutorials and references. Programming and learning new technologies are my passion. The ultimate idea of this site is to share my knowledge(I am still a learner :)) and help you out!. Please spread your words about us (staticreference.com) and give a thumbs up :) Feel free to contact me for any queries!.

6 thoughts on “Scala list flatMap() operation with examples

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.