From Lists to Maps

Lambdas improve your overall experience when working with collections, including Maps.

To create a repeatable set of data, we use the technique shown in Manipulating Lists, where two Lists are zipped and the result is used in a lambda to call a constructor, producing a List of objects:

// FromListsToMaps/People.kt package fromliststomaps data class Person( val name: String, val age: Int ) fun people() = listOf("Alice", "Arthricia", "Bob", "Bill", "Birdperson", "Charlie", "Crocubot", "Franz", "Revolio").zip( listOf(21,15,25,25,42,21,42,21,33)) { name, age -> Person(name, age) }

End of sample. See for full early-access book.

Previous          Next

©2018 Mindview LLC. All Rights Reserved.