I must admit that, for me, one of the most frustrating things when writing Java ccode is the list handling.

Java 8 has some improvements in this respect with Streams, but the syntax in Kotlin, thanks to what we saw earlier on how to compact the code when we have high order functions, greatly simplifies the process.

Functional operations over collections in Kotlin

Although I will not show you all the operations that exist (there are a lot), I want to show you some interesting things you can do with them.

If you want a more complete list, you can have a look at this article I wrote some time ago.

Here are some of the most interesting:

forEach

With it, you can iterate over each element of the collection without further complications:

Remember that, with the use of it, this can be converted into:

viewGroup.children does not exist as such on Android, but we saw in an earlier article how to get this with an extension property.

map

It is an operation that maps each of the objects in a list to a new list, after doing some kind of transformation. Returning to the previous  ViewGroup example:

The map operation in this case maps a range of integers (we’ve already talked about ranges previously), which is a collection, to a new collection of views corresponding to the children of the ViewGroup.

In a later article we’ll see how we can avoid the redundancy of referring to the ViewGroup several times in the same block of code.

filter

As its own name suggests, filter allows us to filter the elements of a collection that meet a specific condition.

For example, if we wanted to filter the child views of a ViewGroup which in turn are a ViewGroup:

For this particular case, we actually have a filterIsinstance that you could use:

There are several more filtering types, such as filterNotNull, that will discard null items, or filterNot, which keeps those that do not meet the condition.

first / last

Related to the previous one, they return the first or last element of the list that fulfils certain condition:

These functions would throw an exception if they don’t find any element that satisfies the condition. An alternative is to use firstOrNull, which will return null in that case.

sort

Sorting elements in Java always becomes a small headache, and the resulting code is not very readable.

However, with Kotlin we can make it much easier, since we can sort by any condition.

Although it may not make much sense, here I will order the view children of a ViewGroup by their visibility. This field is an integer in the View class, so it is very clear which view is larger or smaller than another:

Combination of operations

Of course, all these operations can be combined as we want, in such a way that in very little space we can do quite complex operations:

Here, for example, all views in a ViewGroup that also are ViewGroup are recovered, sorted by visibility, and those that have visibility lower than GONE (ie VISIBLE and INVISIBLE) are taken.

And not only is it very powerful, but it’s a lot easier to read than if you did the same thing with Java 6.

Conclusion

The operations over collections in Kotlin allow us to save many lines of code gaining in expressivity and readability.

This also helps make it much harder to add errors to the code. All pros!

If you want to continue learning about Kotlin, you can get the free guide to learn how to build your first project, or just get the book and learn how to create a complete App from scratch.

Author: Antonio Leiva

I’m in love with Kotlin. I’ve been learning about it for a couple of years, applying it to Android and digesting all this knowledge so that you can learn it with no effort.

Shares
%d bloggers like this: