AssociateBy

This is similar to groupBy, but doesn’t create a List of values in the resulting Map. Instead, the value is always a single element, and, if more than one element satisfies the selector function, the last one in the iteration wins:

val numbers = listOf(1, 2, 3, 4, 5)
val groupped = numbers.associateBy { n -> n.toString() }

In this case, the selector takes the string representation of a number as the key, so the resulting Map looks like this [“1”: 1, “2”:2, “3”:3, “4”:4, “5”:5]

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.43.65