Filter vs Map in Kotlin

Sachin Kumar
1 min readJun 10, 2019

--

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.

--

--

Sachin Kumar
Sachin Kumar

Written by Sachin Kumar

Senior Java Backend Dev | Expertise in Java Microservices, Spring Boot Framework & Android apps development.

Responses (1)