Default Map in Kotlin
Does Kotlin have a default Map? Have you ever used a default map or default dict before? If you know Python a bit, you probably saw its defaultdict in action at some point. Kotlin also comes with a similar tool which I want to demonstrate in this little article. You can find the Python defaultdict documentation and some examples here but the basic use case is shown in the following snippet: from collections import defaultdict d = defaultdict(int) print(d["someKey"]) //0 The defaultdict can also be used with other types and makes sure that you don't get a KeyError when running your code. Instead, it provides a default value for unknown keys, which can be really helpful for grouping and counting algorithms like the following one: from collections import defaultdict data…