Kotlin data mapping: comparison, and map() flatMap() flatten()

While the Android and iOS operating systems are increasingly embracing contemporary functional programming patterns and paradigms introduced by the modern programming languages ​​they use (Kotlin and Swift, respectively), the need to consume and manipulate collections has grown exponentially along with it.

The Kotlin standard library contains a series of extension functions designed to add transformations to collections, which are also designed to meet the growing need for good, fast and efficient collection operations.

How to flash MIUI4.0 system tutorial from the collection provided by Kotlin standard library to Xiaomi? Xiaomi launched MIUI8.2.4.0 Global Stable Rom change. There are four main types: mapping, compression, association and flattening. In this article, I will only focus on two of them; namely Map and Flatten, since this set of extension functions has a special association and is forever compressed together due to the similar use cases they are intended to organize.

track map

Let's start with the basics of maps. The conversion function is defined by Kotlin's official documentation: map()

Mapping transformations create a collection from the results of a function on the elements of another collection. It applies the given lambda function to each subsequent element and returns a list of the lambda results. The order of the result is the same as the original order of the elements.

To better explain what this means, let's start with a simple example:

In this example we have three values: What does recovery mode mean? How to enter the recovery mode of the phone? The numbers 1, 2, and 3 are then processed using a function with a predicate that multiplies each element in the set and returns the following list of results: Set map()numbers``5

[5, 10, 15]

This function has a corresponding function, how to install Bluetooth headset and listen to music on Apple Watch? The AppleWatch Bluetooth headset connection tutorial can make use of the index of the element more conveniently. That said, it is ideal for use when the index of an element may be needed in a desired transformation. map()``mapIndexed()

Here's a simple example, using the same basic set as before, followed by its result: Set

[1, 1, 1]

After Kotlin's null-safety plan, how does the Windows 10 system enter the BIOS? The two extension functions also come with versions of them that allow the resulting collection to ignore all values ​​that might be negated by a given transformation.

Both are created for convenience, keep in mind that null values ​​may still result in some cases, their usage is shown in the next code block: mapNotNull() `` mapIndexedNotNull()

[6, 3, 9]
[6, 3, 9]

Finally, using map transforms with collections opens up two side-by-side options. Transformations to be applied to map keys should use that function, while transformations to be applied to map values ​​should instead be made how to enter the dos interface? How does the computer use the Windows command line (DOS) to use this function. Map mapKeys()mapValues()

As the official docs explain, both functions use transformations that take map entries as arguments, so you can manipulate their keys and values ​​at the same time:

{FIRST=1, SECOND=2, THIRD=3, FOURTH=4}
{first=6, second=8, third=8, fourth=10}

leveling the land

As Kotlin's documentation on collection conversions explains, operating with nested collections sometimes requires the use of standard library functions that provide flat access to elements of nested collections. There are two main functions that provide these types of solutions: and , both of which are considered part of the flatten collection transformation function group. flatten()``flatMap()

First, a function that will take a collection of collections and return a singular containing all the elements the nested collection ever had. flatten()``List

In the first example, we take nested lists of s and flatten them into a single value that just shows all the values: flatten() SetList

[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

This function is able to flatten nested collections containing any type of object type. To further demonstrate the point, here is a very similar example that uses: flatten()``String

[Los Angeles, San Francisco, Sacramento, San Antonio, Houston, Dallas, Mexico City, Monterrey, Guadalajara]

Also, this function is not restricted by the type of objects in its nested collections. This means that the inner collection can contain different object types inside, and the function should still be able to construct a type result that contains all results of each type: flatten() ListAny

[1, 2, 3, one, two, three, 1.0, 2.0, 3.0]

map flat land

Another Flatten collection conversion function provided by Kotlin is . According to its documentation, the second function works very similarly to Hybrid, but provides the additional flexibility of providing a way to handle nested collections by employing a function that maps collection elements to another collection. Thus, a single list of return values ​​for all elements is returned. flatMap() flatten()flatMap()

flatMap()` behaves as a subsequent call — the collection as the result of the map — and . 
— [kotlinlang.org](https://kotlinlang.org/docs/collection-transformations.html#flatten)`map()``flatten()

Let's start with the example we used to test, but add some variations to emphasize the difference between the two options: flatten()

[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

In the example above, we recycled one of the examples, but we are using the function to achieve the same result. flatten()``flatMap()

As can be seen here, the function not only flattens nested collections, but also gets an additional opportunity to add transformations to the inner collection type (different s in this case) for further manipulation via a function fired on the inner collection: flatMap( ) Setmap()

[6, 7, 8, 9, 4, 5, 1, 2, 3, 0]

In the second example, we use the same code again, but this time we'll add additional operations as part of the function steps to further reveal the main differences between the two Flatten functions. flatMap()``map()

This function is especially useful when dealing with complex data classes or POJOs, as it will have the opportunity to look up or further transform information nested under the first level. flatMap()

In the following example, we'll use a mildly complex one to explore more complex data manipulation in calls: data class ``flatMap()

[Austin, San Antonio, Dallas, Houston, Los Angeles, San Francisco, Sacramento, Monterrey, Guadalajara, Mexico City]

Like its sibling, this function is also capable of flattening collections of different types, which are ultimately interpreted as collections of type: flatten() flatMap()Any

[one, two, three, 1, 2, 3]

Finally, the function is also capable of adding additional transformations inside its transformation block, in order to add more transformations to the innermost collection of nested collection groups: flatMap()``map()

[Austin:Texas, San Antonio:Texas, Dallas:Texas, Houston:Texas, Los Angeles:California, San Francisco:California, Sacramento:California, Monterrey:Mexico, Guadalajara:Mexico, Mexico City:Mexico]

The last example shows the pinnacle usage of this function. Will combine with subsequent calls to allow the most general and granular manipulations of lists while providing the user with a flat interpretation of such lists for any kind of need. flatMap() flatMap()map()

in conclusion

All three Kotlin data mapping functions ( , , and ) are part of a set of transformation functions that deal specifically with Kotlin collections. They help in interpreting and manipulating complex and/or nested collections which can be difficult to analyze in their default state. map() flatten()flatMap()

By properly utilizing any of these powerful features, developers should be better able to extract the desired information from any type of complex collection structure. This technique is critical to maximizing the efficiency and reliability of developer work. Getting these right can come in handy when dealing with the popular reactive streaming coding paradigm, and popular concurrency libraries like RxJava and Kotlin coroutines enable you to do just that.

Guess you like

Origin blog.csdn.net/weixin_47967031/article/details/132575541