Topic 1 Extra Material


This page consists of additional reading on basic Kotlin covering some further features. We will not have time to cover this in the lecture but it is provided for you to read.


Mappings: performing the same operation on all members of a list

Another common use of lambdas is to perform a mapping. A mapping transforms each member of an input list by a specified function, and returns a new list containing the transformed data. This example will convert each string in the input list to lower case and return a new list containing the lower case values:

fun main(args: Array<String>) {
    val peopleList = listOf("Mark Cranshaw", "Rob Cooper", "Al Monger", "Mark Udall", "Margaret Jones")
    val lowerCaseList = peopleList.map { person -> person.lowercase() }
    println(lowerCaseList)    
}
Note how we use the map() function to transform each member of peopleList by a specified lambda. The lambda here will take each member of the input list in turn (person) and return that member converted to lower case (i.e. person.lowercase()). So, the list returned from map, i.e. lowerCaseList, will contain the person names converted to lower case.


Inheritance


The Map


Creating a mutable Map


forEach() on maps


Other collection functions which use lambdas


Other collection functions - example