Book Image

Programming Kotlin

Book Image

Programming Kotlin

Overview of this book

Quickly learn the fundamentals of the Kotlin language and see it in action on the web. Easy to follow and covering the full set of programming features, this book will get you fluent in Kotlin for Android.
Table of Contents (20 chapters)
Programming Kotlin
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Customer Feedback
Preface

Maps


A map collection, as the name implies, allows you to associated an object (key) to another object (value). A map dictates that your collection can't contain duplicate keys, and each key is mapped to at most one value. The interesting part about a map is that its interface provides three collection views: the set of keys, the collection of all the values, and the set of key-value mappings.

When using a map, you need to pay attention to the keys you are using. When adding an item to the map, first thing it does is to locate which bucket it should go into. To do so, it will use the hashCode method, and after that, depending on the implementation, it will use the equals method. Therefore, your keys need to be immutable, otherwise the behavior of the map can't be specified.

We know already that Kotlin provides support for immutable and mutable maps at the interface level. This is reflected in the collection API since there are specific methods for each flavor of map:

    data class Customer...