Chao Shi
Chao Shi
Jul 18 · 3 min read

This article is designed to explain three important concepts in functional programming — map, filter, and reduce. I will use some simple examples and make a comparison between Java and Kotlin for a clearer explanation.

Map

The map function is used to transfer a given collection to another collection. Here is an example:

We are trying to save a list of strings to another string:

// Java codeString[] names = {"James", "Tom", "Jack", "Kobe"};int[] namesLength = new int[names.length];for (int i = 0; i < names.length ; i ++) {  namesLength[i] = names[i].length();}

The Kotlin code for this would be:

// Kotlin codeval names = arrayOf("James", "Tom", "Jack", "Kobe");val namesLength = names.map { it.length }

An example of using this would be when you want to transfer a HashMap to a List:

val params = mapOf("limit" to "10", "offset" to "20", "order" to "asc")val s = params.map { "${it.key}=${it.value}" }.joinToString("&")

Filter

The filter function is used for returning a sub-collection from a given collection.

For example, if the logic is to filter the number, which is a multiple value of 3, the Java code would be:

// Java codeint[] all = {1, 2, 3, 4, 5, 6, 7, 8, 9};List<Integer> filter = new ArrayList<>();for (int a : all) {  if (a % 3 == 0) {    filters.add(a);  }}

Kotlin can make it quite simple:

// Kotlin codeval all = arrayOf(1, 2, 3, 4, 5, 6, 7, 8, 9)val filters = all.filter { it % 3 == 0 }

Besides this one, Kotlin provides some other filter functions:

  1. FilterIndex is very similar to filter, but we can get the current item index when doing the logic.
  2. FilterNot is the opposite of the function to filter — it only returns the values that do not meet the logic.
  3. For the Map type collection, Kotlin provides the function called filterKeys and filterValues, to make it easy for the filtering of only key or value.

Reduce

This is used if we want to combine the first character from each string to a new string.

// Java codeString[] texts = {"asset", "bitt", "catt", "doggy"};StringBuffer sb = new StringBuffer();for (int i = 0; i < texts.length ; i ++) {  sb.append(texts[i].substring(0, 1));}String result = sb.toString();

The Kotlin code would be:

// Kotlin codeval texts = arrayOf("asset", "bitt", "catt", "doggy")val result = texts.map { it.substring(0,1) }.reduce { r, s -> "$r$s"}

and the value returned would be “abcd.”


As ever, QuarkWorks is available to help with any software application project — web, mobile, and more! If you are interested in our services you can check out our website. We would love to answer any questions you have! Just reach out to us on our Twitter, Facebook, or LinkedIn.

QuarkWorks, Inc.

Build better software.

Chao Shi

Written by

Chao Shi

Android developer

QuarkWorks, Inc.

Build better software.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade