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

<p>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.</p> <p>Let&rsquo;s begin with :</p> <h1>.flatten()</h1> <p><img alt="" src="https://miro.medium.com/v2/resize:fit:700/1*MAoAszM7F3-4rCjhk2gzbw.png" style="height:289px; width:700px" /></p> <p>This is the most simple way of flattening a list of lists into just list. Just call&nbsp;<code><strong>.flatten()</strong></code>&nbsp;on the receiver collection to have a flat list.</p> <h1>.flatMap()</h1> <p><img alt="" src="https://miro.medium.com/v2/resize:fit:700/1*d4MdBGX8kQj55kCS-2aPzg.png" style="height:285px; width:700px" /></p> <p>Or, if you want to apply some kind of transformation whilst flattening, you can use&nbsp;<code><strong>.flatMap()</strong></code>.</p> <p><a href="https://medium.com/@hossein.ameri/mastering-kotlin-a-collection-of-essential-functions-every-developer-should-know-with-examples-c46e414bd683"><strong>Read More</strong></a></p>