Mastering Kotlin: A Collection of Essential Functions Every Developer Should Know with Examples ??? ??? -Part 4 -Flattening

Flattening in general is removing the nestedness in a collection. Consider you are given a list of lists. If you want to remove all child lists and get their elements into one single-level list, you need to flatten that list.

Let’s begin with :

.flatten()

This is the most simple way of flattening a list of lists into just list. Just call .flatten() on the receiver collection to have a flat list.

.flatMap()

Or, if you want to apply some kind of transformation whilst flattening, you can use .flatMap().

Read More