Learning Kotlin: The map delegate

Submitted by Robert MacLean on Thu, 08/30/2018 - 09:00
**More Information** * This is the 28th post in a multipart series. If you want to read more, see our [series index](/learning-kotlin-introduction) * This follows the [introduction to the `by` operator and delegates](/learning-kotlin-by-delegate-properties)

The next delegate is actually built into something we have looked at before, map and if you use by to refer to one, when you get the value it goes to the map values to look it up.

As a way to illustrate this, we will take some JSON data and parse it with the kotlinx.serialization code. We then loop over each item and convert it into a map which we use to create an instance of User.

  1. import kotlinx.serialization.json.*
  2.  
  3. class User(userValues:Map<String, Any?>) {
  4.     val name: String by userValues
  5.     val eyeColour: String by userValues
  6.     val age : String by userValues
  7. }
  8.  
  9. fun main(args:Array<String>) {
  10.     val json = """[{
  11.         "name":"Robert",
  12.         "eyeColour":"Green",
  13.         "age":36
  14.     },{
  15.         "name":"Frank",
  16.         "eyeColour":"Blue"
  17.     }]""";
  18.  
  19.     (JsonTreeParser(json).read() as JsonArray)
  20.         .map{  
  21.             val person = it as JsonObject
  22.             person.keys.associateBy({it}, {person.getPrimitive(it).content})
  23.         }.map {
  24.             User(it)
  25.         }.forEach { println("${it.name} with ${it.eyeColour} eyes") }
  26.        
  27. }

This code outputs:

Robert with Green eyes
Frank with Blue eyes

However, if we change line 25 to include the age we get a better view of what is happening.

  1. }.forEach { println("${it.name} with ${it.eyeColour} eyes and is ${it.age} years old") }

This simple adjustment changes the output to be:

Robert with Green eyes and is 36 years oldException in thread "main"
java.util.NoSuchElementException: Key age is missing in the map.
        at kotlin.collections.MapsKt__MapWithDefaultKt.getOrImplicitDefaultNullable(MapWithDefault.kt:24)
        at sadev.User.getAge(blog.kt)
        at sadev.BlogKt.main(blog.kt:27)

As you can see, the map is not initialising the values ahead of time. Rather it is merely being used to look up what value in the map when the properties getter is called.