<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Ali Eren Şen</title>
    <description>The latest articles on DEV Community by Ali Eren Şen (@alieren).</description>
    <link>https://dev.to/alieren</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F942474%2F65e3841e-d07d-4df4-af26-bb312baef3da.jpeg</url>
      <title>DEV Community: Ali Eren Şen</title>
      <link>https://dev.to/alieren</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/alieren"/>
    <language>en</language>
    <item>
      <title>Transforming Kotlin Collections - Functions with Examples</title>
      <dc:creator>Ali Eren Şen</dc:creator>
      <pubDate>Thu, 13 Oct 2022 07:07:23 +0000</pubDate>
      <link>https://dev.to/appcircle/transforming-kotlin-collections-functions-with-examples-21ao</link>
      <guid>https://dev.to/appcircle/transforming-kotlin-collections-functions-with-examples-21ao</guid>
      <description>&lt;p&gt;Knowing about the standard library functions in Kotlin saves a lot of time while working on complex data structures. Whether you are mapping the API response model into your business model or you are sorting, filtering, and manipulating data; knowing  these operation on Kotlin collections come really handy.&lt;/p&gt;

&lt;p&gt;In this post, we are gonna go through some collection transformation operations that I use frequently.&lt;/p&gt;

&lt;h2&gt;Kotlin Collections - Transformation Operations&lt;/h2&gt;

&lt;h2&gt;1. Mapping&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fac.appcircle.io%2Fwp-content%2Fuploads%2F2022%2F10%2FMapping1-1-1030x515.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fac.appcircle.io%2Fwp-content%2Fuploads%2F2022%2F10%2FMapping1-1-1030x515.png" alt="" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Mapping means to modify each item in a collection by applying a transformation lambda which results in the creation of another collection.&lt;/p&gt;

&lt;p&gt;If you are working on lists of data that you need to transform from one model into another or if you want to convert the API response model into a domain layer model you should use mapping operations in Kotlin.&lt;/p&gt;

&lt;h3&gt;.map()&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fac.appcircle.io%2Fwp-content%2Fuploads%2F2022%2F10%2FMapping2-1030x515.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fac.appcircle.io%2Fwp-content%2Fuploads%2F2022%2F10%2FMapping2-1030x515.png" alt="" width="800" height="400"&gt;&lt;/a&gt;&lt;br&gt;
Using the &lt;code&gt;.map()&lt;/code&gt; function is the most common way to convert a collection of items into another. It applies the given transform lambda to each of the items of the receiver collection and yields them into a newly created list.&lt;/p&gt;

&lt;h3&gt;.mapIndexed()&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fac.appcircle.io%2Fwp-content%2Fuploads%2F2022%2F10%2FMapping3-1030x515.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fac.appcircle.io%2Fwp-content%2Fuploads%2F2022%2F10%2FMapping3-1030x515.png" alt="" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you also need the index of each item when transforming them, you can use the &lt;code&gt;.mapIndexed()&lt;/code&gt; function. It will feed the index of each item to the transform lambda so that you can utilize the order of the items.&lt;/p&gt;

&lt;h3&gt;.mapNotNull()&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fac.appcircle.io%2Fwp-content%2Fuploads%2F2022%2F10%2FMapping4-1030x515.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fac.appcircle.io%2Fwp-content%2Fuploads%2F2022%2F10%2FMapping4-1030x515.png" alt="" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Another convenient mapping function is &lt;code&gt;.mapNotNull()&lt;/code&gt;. You can use it if you want to filter out null items after the conversion.&lt;/p&gt;

&lt;p&gt;For example, if you have a list of objects which may or may not have text property, you can use &lt;code&gt;.mapNotNull()&lt;/code&gt; to get a list of texts without any nulls being in it.&lt;/p&gt;

&lt;p&gt;--&lt;/p&gt;

&lt;p&gt;Kotlin's standard library also has &lt;code&gt;.mapIndexedNotNull()&lt;/code&gt; method which combines &lt;code&gt;.mapIndexed()&lt;/code&gt; and &lt;code&gt;.mapNotNull()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Also you can find &lt;code&gt;.mapKeys()&lt;/code&gt; and &lt;code&gt;.mapValues()&lt;/code&gt; methods which are defined on &lt;code&gt;Map&lt;/code&gt;s.&lt;/p&gt;

&lt;p&gt;Please check out the &lt;a href="https://kotlinlang.org/docs/collection-transformations.html#map" rel="noopener noreferrer"&gt;official documentation&lt;/a&gt; if you need more information on the topic.&lt;/p&gt;

&lt;h2&gt;2. Zipping&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fac.appcircle.io%2Fwp-content%2Fuploads%2F2022%2F10%2FZipping1-1-1030x515.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fac.appcircle.io%2Fwp-content%2Fuploads%2F2022%2F10%2FZipping1-1-1030x515.png" alt="" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When you have two lists of the same size and you want to merge each item from the first list with the corresponding item coming from the second list you want zipping.&lt;/p&gt;

&lt;p&gt;Kotlin standard library has convenient functions to zip and unzip collections including &lt;code&gt;List&lt;/code&gt;s, &lt;code&gt;Array&lt;/code&gt;s, and &lt;code&gt;Sequence&lt;/code&gt;s.&lt;/p&gt;

&lt;p&gt;Let's take a look at what we have:&lt;/p&gt;

&lt;h3&gt;.zip()&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fac.appcircle.io%2Fwp-content%2Fuploads%2F2022%2F10%2FZipping2-1030x515.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fac.appcircle.io%2Fwp-content%2Fuploads%2F2022%2F10%2FZipping2-1030x515.png" alt="" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can use &lt;code&gt;.zip()&lt;/code&gt; without any transformation to create a list of pairs. The pairs have their first item from the receiver list and the second from the argument. If you want to combine two different data sources into one and if the sources has the same number of items, the function &lt;code&gt;.zip()&lt;/code&gt; is really useful.&lt;/p&gt;

&lt;h3&gt;.zip() with transform lambda&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fac.appcircle.io%2Fwp-content%2Fuploads%2F2022%2F10%2FZipping3-1030x515.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fac.appcircle.io%2Fwp-content%2Fuploads%2F2022%2F10%2FZipping3-1030x515.png" alt="" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you don't need pairs but you want to define your own zipping logic, good news: &lt;code&gt;.zip()&lt;/code&gt; can also take a transform lambda as an argument. So you don't need to chain a &lt;code&gt;.zip()&lt;/code&gt; command with a &lt;code&gt;.map()&lt;/code&gt;. Instead, pass a lambda and combine two items coming from two data sources in a way you would prefer.&lt;/p&gt;

&lt;h3&gt;.unzip()&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fac.appcircle.io%2Fwp-content%2Fuploads%2F2022%2F10%2FZipping4-1030x515.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fac.appcircle.io%2Fwp-content%2Fuploads%2F2022%2F10%2FZipping4-1030x515.png" alt="" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Another great function in Kotlin is the &lt;code&gt;.unzip()&lt;/code&gt;. The name gives it all away: it does the opposite of &lt;code&gt;.zip()&lt;/code&gt;. If you have a list of pairs and want to create two lists that contain the firsts and seconds of each item, then just &lt;code&gt;.unzip()&lt;/code&gt; it!&lt;/p&gt;

&lt;p&gt;--&lt;/p&gt;

&lt;p&gt;If you want to see some examples please check out the &lt;a href="https://kotlinlang.org/docs/collection-transformations.html#zip" rel="noopener noreferrer"&gt;official documentation&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Now let's check out another group of transformation operations:&lt;/p&gt;

&lt;h2&gt;3. Association&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fac.appcircle.io%2Fwp-content%2Fuploads%2F2022%2F10%2FAssociation1-1-1030x515.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fac.appcircle.io%2Fwp-content%2Fuploads%2F2022%2F10%2FAssociation1-1-1030x515.png" alt="" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Association is when you want to traverse a &lt;code&gt;List&lt;/code&gt; and convert it into a &lt;code&gt;Map&lt;/code&gt; by associating each item with one of its properties. Understanding association operations is easier to show than tell, so let's take a look at what sort of functions we have in the standard library:&lt;/p&gt;

&lt;h3&gt;.associateWith()&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fac.appcircle.io%2Fwp-content%2Fuploads%2F2022%2F10%2FAssociation2-1030x515.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fac.appcircle.io%2Fwp-content%2Fuploads%2F2022%2F10%2FAssociation2-1030x515.png" alt="" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;.associateWith()&lt;/code&gt; function, structures the items of the List as keys of a &lt;code&gt;Map&lt;/code&gt;. The values for each key are computed by running the given lambda on the item. Please note that maps cannot have duplicate keys, so if the receiver collection has repetitive items, only the last one will remain in the map.&lt;/p&gt;

&lt;h3&gt;.associateBy()&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fac.appcircle.io%2Fwp-content%2Fuploads%2F2022%2F10%2FAssociation3-1030x515.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fac.appcircle.io%2Fwp-content%2Fuploads%2F2022%2F10%2FAssociation3-1030x515.png" alt="" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Kotlin also has &lt;code&gt;.associateBy()&lt;/code&gt;, which functions almost the same. The difference is items of the receiver collections are not used as keys but values.&lt;/p&gt;

&lt;p&gt;That means if the lambda returns the same values for different items, only the last one will be present in the resulting map.&lt;/p&gt;

&lt;h3&gt;.associate()&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fac.appcircle.io%2Fwp-content%2Fuploads%2F2022%2F10%2FAssociation4-1030x515.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fac.appcircle.io%2Fwp-content%2Fuploads%2F2022%2F10%2FAssociation4-1030x515.png" alt="" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The last associating function in the standard library is &lt;code&gt;.associate()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Using this function, you can define the logic of building the map. You need to pass a lambda that returns &lt;code&gt;Pair&lt;/code&gt;s and a &lt;code&gt;Map&lt;/code&gt; will be built for you using these &lt;code&gt;Pair&lt;/code&gt;s.&lt;/p&gt;

&lt;p&gt;Please note that this function has some performance implications since it generates &lt;code&gt;Pair&lt;/code&gt;s as an intermediate step.&lt;/p&gt;

&lt;p&gt;--&lt;/p&gt;

&lt;p&gt;If you want to learn more on association you can take a look at the &lt;a href="https://kotlinlang.org/docs/collection-transformations.html#associate" rel="noopener noreferrer"&gt;official documentation&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Now, let's see how we can flatten collections:&lt;/p&gt;

&lt;h2&gt;4. Flattening&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fac.appcircle.io%2Fwp-content%2Fuploads%2F2022%2F10%2FFlattening1-1-1030x515.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fac.appcircle.io%2Fwp-content%2Fuploads%2F2022%2F10%2FFlattening1-1-1030x515.png" alt="" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;Let's begin with:&lt;/p&gt;

&lt;h3&gt;.flatten()&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fac.appcircle.io%2Fwp-content%2Fuploads%2F2022%2F10%2FFlattening2-1030x515.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fac.appcircle.io%2Fwp-content%2Fuploads%2F2022%2F10%2FFlattening2-1030x515.png" alt="" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is the most simple way of flattening a list of lists into just list. Just call &lt;code&gt;.flatten()&lt;/code&gt; on the receiver collection to have a flat list.&lt;/p&gt;

&lt;h3&gt;.flatMap()&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fac.appcircle.io%2Fwp-content%2Fuploads%2F2022%2F10%2FFlattening3-1030x515.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fac.appcircle.io%2Fwp-content%2Fuploads%2F2022%2F10%2FFlattening3-1030x515.png" alt="" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Or, if you want to apply some kind of transformation whilst flattening, you can use &lt;code&gt;.flatMap()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Please note that &lt;code&gt;.flatMap()&lt;/code&gt; is just a shorter form of &lt;code&gt;.map().flatten()&lt;/code&gt; chain.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fac.appcircle.io%2Fwp-content%2Fuploads%2F2022%2F10%2FFlattening4-1030x515.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fac.appcircle.io%2Fwp-content%2Fuploads%2F2022%2F10%2FFlattening4-1030x515.png" alt="" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;--&lt;/p&gt;

&lt;p&gt;If you want to see some code examples, you may check the &lt;a href="https://kotlinlang.org/docs/collection-transformations.html#flatten" rel="noopener noreferrer"&gt;official documentation&lt;/a&gt; on flattening.&lt;/p&gt;

&lt;p&gt;Now let's check out last chapter:&lt;/p&gt;

&lt;h2&gt;5. String Representation&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fac.appcircle.io%2Fwp-content%2Fuploads%2F2022%2F10%2FString1-1-1030x515.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fac.appcircle.io%2Fwp-content%2Fuploads%2F2022%2F10%2FString1-1-1030x515.png" alt="" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you are generating user-visible text out of Kotlin collections or trying to log an internal state you will need to represent collections as strings. I really liked when I learned that Kotlin standard library has a function just for this reason:&lt;/p&gt;

&lt;h3&gt;.joinToString()&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fac.appcircle.io%2Fwp-content%2Fuploads%2F2022%2F10%2FString2-1030x515.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fac.appcircle.io%2Fwp-content%2Fuploads%2F2022%2F10%2FString2-1030x515.png" alt="" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If don't pass any parameters to &lt;code&gt;.joinToString()&lt;/code&gt;, string representations of each items will be concatenated in a string and separated by commas with spaces by default.&lt;/p&gt;

&lt;h3&gt;Customizing the separator, prefix and postfix strings&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fac.appcircle.io%2Fwp-content%2Fuploads%2F2022%2F10%2FString3-1030x515.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fac.appcircle.io%2Fwp-content%2Fuploads%2F2022%2F10%2FString3-1030x515.png" alt="" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;However, if you need to change the separator strings or add some more useful information to the start or to the end you can pass some arguments for &lt;code&gt;separator&lt;/code&gt;, &lt;code&gt;prefix&lt;/code&gt; and &lt;code&gt;postfix&lt;/code&gt; parameters.&lt;/p&gt;

&lt;h3&gt;The limit and truncated arguments&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fac.appcircle.io%2Fwp-content%2Fuploads%2F2022%2F10%2FString4-1030x515.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fac.appcircle.io%2Fwp-content%2Fuploads%2F2022%2F10%2FString4-1030x515.png" alt="" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;.joinToString()&lt;/code&gt; also support limiting and truncating the list. If you have a long list and don't want to print everything and make a mess, you can limit how many elements you want in the resulting string and what string to use in the place of an ellipsis.&lt;/p&gt;

&lt;p&gt;--&lt;/p&gt;

&lt;p&gt;Finally, it also supports passing a transform lambda if you also want to define how each item will be converted into a string.&lt;/p&gt;

&lt;p&gt;You can study the &lt;a href="https://kotlinlang.org/docs/collection-transformations.html#string-representation" rel="noopener noreferrer"&gt;official documentation&lt;/a&gt; to learn more about the topic.&lt;/p&gt;




&lt;p&gt;That was it! Now you have learned how to transform Kotlin collections using a handful of convenience functions.&lt;/p&gt;

&lt;p&gt;If you like the article please share in your social media and if you want to learn more on Kotlin's standard library functions, let us know!&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
