
Because of this, we were free to populate this map with keys and values belonging to varied data types. We have created a Kotlin map without specifying any data type for its keys and values in this script. For this example, we will make use of the Kotlin script shown in the image below: Now, we will create a generic Kotlin map and print all of its keys and values on the terminal. To compile this Kotlin program, the following command was used:Īll the values of our Kotlin map are shown in the following image:Įxample # 2: Creating a Generic Kotlin Map and Printing Its Keys and Values on the Terminal: Within this loop, we have printed the values corresponding to each of these keys on the terminal. Then, we have used a “for” loop for iterating through all the keys of this map. We have populated this map with three different key-value pairs. We wanted this map to be of “” type, i.e., integer IDs and string names. In this Kotlin script, we have first created a map of different courses and their IDs with the help of the “mapOf” function. However, in this example, we will be creating a type-specific map in Kotlin and will print its values on the terminal, as depicted in the Kotlin script shown below: Whereas in the latter case, you can have varied data types for the different keys and values of the same map. In the former case, all the key-value pairs of the map are of the same data type. The maps in Kotlin can have a pre-determined data type, or they can be generic. How to Use the Maps in Kotlinįor making use of the Kotlin maps in Ubuntu 20.04, you can consider the following five examples as a helping guide: Example # 1: Creating a Type-Specific Kotlin Map and Printing Its Values on the Terminal: In this article, we will try to understand the usage of the maps in Kotlin with the help of different examples in Ubuntu 20.04.

The keys and values can be of the same data type or diverse data types. A map in Kotlin is simply a container having key-value pairs.

Like other programming languages, Kotlin also provides the programmers with the capability of creating and using maps.
