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.
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.
open class Person(var name: String, var age: Int, var weight: Int) { open fun display() { println("Name $name Age $age Weight $weight") } } class Employee(n: String, a: Int, w: Int, var jobTitle: String, var salary: Double) : Person(n,a,w) { override fun display() { super.display() // as in Java println("Job title $jobTitle Salary $salary") } } fun main (args: Array<String>) { val jake = Person("Jake", 4, 30) val tony = Employee("Tony", 60, 100, "Head of Biochemistry", 70000.0) jake.display() tony.display() }
class Employee(n: String, a: Int, w: Int, var jobTitle: String, var salary: Double) : Person(n,a,w)The Employee constructor takes five parameters, three of which (n, a and w) are passed up to the Person constructor (hence Person(n,a,w)) and two of which are declared attributes of Employee (jobTitle and salary)
fun main(args: Array<String>) { val olympus = mutableMapOf("name" to "Mount Olympus", "height" to "2917m", "country" to "Greece") println("Name: ${olympus["name"]}, height: ${olympus["height"]}, highest in: ${olympus["country"]}") // Add another key/value pair to the Map olympus["comment"] = "Legendary home of the gods." println(olympus["comment"]) }
fun main(args: Array<String>) { val olympus = mapOf("name" to "Mount Olympus", "height" to "2917m", "country" to "Greece") olympus.forEach { println("The ${it.key} of Mount Olympus is ${it.value}.") } }Each key/value pair within the Map is passed in turn to the specified lambda function as a Pair object (shown here as the implicit it parameter)
data class Employee(val name: String, val salary: Double) fun main(args: Array<String>) { listOf(111, 222, 333, 555, 666, 444, 888, 777).filter { it < 500}.forEach { println(it) } listOf(Employee("Tom", 28000), Employee("Jane", 32000)).map{ Employee(it.name, it.salary*1.2)}.forEach{ println("${it.name} ${it.salary}") }Note how we can chain these functions together, e.g. filter() followed by forEach() in the first example