Filter vs Map in Kotlin
In this article, we are going to discuss the concepts of Filter and Map in Kotlin using the following code snippets. Suppose we have a list of integer elements and we want to retrieve only the elements as per our requirement:
Now, we are only interested in the elements which are less than 4, then here filter will be useful:
The output of the above code will be:
1
2
3
But when it comes to transforming the elements in the list, Map plays a vital role in that. Suppose we want to convert each element in the list into the square of themselves:
Output:
1
4
9
16
25
As we can see above all the elements have been transformed into the square of themselves.
This is the main difference between Filter and Map in Kotlin.
If you’ve liked this short article, please give us a clap and share this with others.