![]() This program counts the number of times certain words appear from an except of “Green Eggs and Ham” by Dr. Change phoneNumbers into a read only mapĬouting the frequency of words in text is a common use case for maps. We can protect ourselves from such accidents by converting a mutableMap into a an immutableMap. This normally isn’t a problem, but it can end up being a source of bugs, especially for new learners. If we use put() with a key that already exists, we will overwrite the current value of the map. It takes the key as the only argument and removes the entry from the map. The remove method is the opposite operation. ![]() The first argument is the key and the second argument is the value. In this case, we use the put() method to add items to the map. PhoneNumbers.put("Linda Belcher", 8675310) Type is needed when we do not specify arguments in mutableMapOf() Let’s look at a code example to see how we add items and remove them from a mutableMap. This is the read/write version of the map that has methods to add items and remove items from a map. When we do not know all of the values we are placing in the map ahead of time, we can use mutableMapOf(). In other words, to print Bob’s number, we would write phoneBook. When we need to access an item in the map, we use the index operator and put the key inside of the operator. Each pair is written key to value so in our example, “Bob Belcher” to 8675309 or “Linda Belcher” to 8675309. We create a phoneBook object using the mapOf(), or mutableMap(), and then specifying any number of pair arguments. ![]() ![]() Val phoneBook = mapOf("Bob Belcher" to 8675309, "Linda Belcher" to 8675310) In Kotlin, we could make such a map like so: In the phone book example, we can think of a person’s name as the key and the phone number as the value. suspend fun updateIntance(dbInstanceIdentifierVal: String, masterUserPasswordVal: String. Find the complete example and learn how to set up and run in the AWS Code Examples Repository. ![]() When we want to call somebody, and we only know their name but not their phone number, we find their name in the phone book and the phone book has their number associated with a name. This is prerelease documentation for a feature in preview release. It’s easy to think of a map being like a phone book. When we lookup values in a map, we specify a key and the map returns an associated value. Maps are data structures that associate keys with values. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |