Function operations and collections in Kotlin (KAD 11 ),
By Antonio Leiva
Time: Feb 2, 2017
Link: https://antonioleiva.com/functional-operations-collections-kotlin/
For me, I must admit that using Java code to process a list is one of the most frustrating tasks.
In this regard, although Java 8 has made some improvements in using Stream, as we have seen earlier on how we use high-order functions to compress code, the Kotlin syntax greatly simplifies this process.
Integrated functions in Kotlin
Although I don't show you all the existing operations (there are many), I want to show you some of them, and you can use them to do some interesting things.
If you need a more complete list, you can find it in the article I wrote earlier.
The most interesting ones are:
ForEach
With forEach, you can simply iterate each element of the Set:
1 viewGroup.children.forEach { v -> v.visibility = VISIBLE }
Remember, with it, this can also be converted:
1 viewGroup.children.forEach { it.visibility = VISIBLE }
No viewGroup. children Exists in Android, but I have seen it in the previous article "how to obtain extended attributes.
Map
This operation maps each object in the list to a new list, and then performs some conversion. Return to the previous ViewGroup example:
1 val childViews = (0..viewGroup.childCount - 1).map { viewGroup.getChildAt(it) }
In this case, the map operation maps the Integer Range of a set (previously discussed) to the new gallery corresponding to the ViewGroup subview.
In subsequent articles, we will explain how to avoid redundant reference to ViewGroup multiple times in the same code block.
Filter
As shown in its name, filter allows us to filter elements in a set based on specific conditions.
For example, the ViewGroup sub-views to be filtered are ViewGroup in sequence:
1 val childViews = viewGroup.children.filter { it is ViewGroup }
In this special case, we can actually use filterIsinstance:
1 val childViews = viewGroup.children.filterIsInstance<ViewGroup>()
There are several filtering types, such as filterNotNull, which discard null items; or filterNot, retaining items other than the specified conditions.
First/last
Related to the previous item, they return the first or last element of the list that meets certain conditions:
1 val firstTextView = viewGroup.children.first { it is TextView }2 val lastTextView = viewGroup.children.last { it is TextView }
If they do not find any element that meets the conditions, these functions throw an exception. In this case, you can use the replacement method firstOrNull, which returns null.
Sort
In Java, sorting elements always have a headache and the code is poorly readable. However, with Kotlin, we can sort it by any condition, which is much easier.
Although this example may not make much sense, I can sort ViewGroup subviews by visibility. In the View class, the visibility field is an integer, so it is clear that one view is larger or smaller than the other:
1 val firstTextView = viewGroup.children.sortedBy { it.visibility }
Operation combination
Of course, all these operations can be combined according to our requirements. In this way, we can use a small amount of code to perform very complex operations:
1 (0..viewGroup.childCount - 1)2 .map { viewGroup.getChildAt(it) }3 .filterIsInstance()4 .sortedBy { it.visibility }5 .takeWhile { it.visibility < View.GONE }
For example, all ViewGroup views in the ViewGroup are restored and sorted by visibility to indicate those with lower visibility than GONE (that is, Visible and INVISIBLE ).
This is not only very powerful, but also easier to read than writing the same function in Java 6.
Conclusion
In Kotlin, operations on collections save a lot of code lines for better expression and readability.
This helps us to reduce coding errors. Great!
If you want to continue learning Kotlin, you can get a free guide to learn how to build your first project, or get this book, and learn how to start from scratching your head to creating a complex APP.