June 2018
Intermediate to advanced
310 pages
6h 32m
English
Your boss—sorry, scrum master—came to you yesterday with an urgent requirement. From now on, all map data structures in your system are to become HappyMaps.
What, you don't know what HappyMaps are? They are the hottest stuff going around right now. They are just like the regular HashMap, but when you override an existing value, they print the following output:
Yay! Very useful
So, what you do is type the following code in your editor:
class HappyMap<K, V>: HashMap<K, V>() { override fun put(key: K, value: V): V? { return super.put(key, value).apply { this?.let { println("Yay! $key") } } }}
We've seen apply() already when we discussed the Builder design pattern in the previous chapter and this?.let { ... } is a nicer way ...
Read now
Unlock full access