Hashmaps
Kotlin HashMap
s are interesting; they are a type of cousin to ArrayList
. They encapsulate useful data storage techniques that would otherwise be quite technical for us to code successfully ourselves. It is worth looking at HashMap
before getting back to the Note to self app.
Suppose that we want to store the data of lots of characters from a role-playing game and each different character is represented by an object of the Character
type.
We could use some of the Kotlin tools that we already know about, such as arrays or ArrayList
. However, with HashMap
, we can give a unique key or identifier to each Character
object, and access any such object using that same key or identifier.
Note
The term "hash" comes from the process of turning our chosen key or identifier into something used internally by the HashMap
class. The process is called hashing.
Any of our Character
instances can then be accessed with our chosen key or identifier. A good candidate for a key or identifier in the...