Maps in Kotlin Android

Start course
1h 43m

This course covers the fundamentals of Kotlin, looking at the building blocks of the programming language and how they can be used to build apps in Android.

Intended Audience

This course is ideal for anyone who wants to learn how to use Kotlin for developing applications on Android.


This content will take you from a beginner to a proficient user of Kotlin and so no prior experience with the programming language is required. It would, however, be beneficial to have some development experience in general.


Well hello, my friends. So, we learned the set in our previous lesson. That means in this lesson, we're going to learn the map. The feature that distinguishes map from other collections is that we can store elements in the array as key value pairs. So, let's examine our example on the screen. So, after specifying the name of the array, we write mapOf to the right of the equal sign. Between the chevron marks, we need to write two different data types. So, the first data type will be the type of key  that represents the element we will add to the array and the second data type will be the type of element that we want to add to the array.

So, in our example here, the key's type is string. The elements type is integer. All right? So, we now need to write the key and value parameters within parentheses. So, in this example we've got two elements. The key of the first element is david and its value is 20. The key of the second element is ronaldo and that value would be 25. So, now let's see how to print the elements of an array created with mapOf to the console. So, in our example to print the value 20 to the console we will need to use the David key. And of course, similarly to print the value 25 to the screen, we will need to use the Ronaldo Key.

Cool. So now, that you have the lowdown about map, why don't we open up Android Studio and practice it a little bit? So first, I'll create a Kotlin file, brand new, specify the name, Collections Map. So now, let's create the main method. So, I said the name of the array to be age. Right, the mapOf between the chevron marks. I must first write the keys data type. So here, I am writing string. So now, I've got to determine the data type of the element that I will add to the array. So, here I can write Int. Of course, you can specify any data type that you want here. But now, let's try to add the elements of the array inside the parentheses. 

So, David will be the key of the first element. David's age will be 20. We associate the key and value pair with each other using the two statement. All right? So, this is how you can store people's ages. Just like this example. You specify contact names as keys. You can also specify people's ages as values. So now, let's add the second element. So, I'm typing Ronaldo is a key and Ronaldo's age will be 25. Okay so, we've added the ages of David and Ronaldo to the array as a key value pair.

Now, let's print these elements to the console. I'll type out and press enter and I'll write the age of David in parentheses between double quotes. And after I put the plus sign in, I type age and open square brackets. We should write the key here as well. That's why I'm writing david here is a string expression. Now, lets print ronaldo's age on the console so I can copy and paste this line of code. I'm typing Ronaldo here and I'm correcting the key here to be Ronaldo. All right. So, now let's run our code. Right? So, as you can see, we have printed the ages of David and Ronaldo right here to the console.

All right, my friends. So, that's how map works. But I do want to talk to you about one more thing. Notice that we added the elements of the array while defining the array. So, can we add an element to the array later? Or can we remove any element of the array from the array? Let's have a look at it now. So, when I type age.add or age.put here. No method appears because set and map collections are also divided by mutable and immutable. Right? The mapOf that we use here is actually an immutable collection. So, that is you have to specify the elements of the array at the beginning and then after you create the array, you cannot make any changes to the array. If you do want to make changes to the array later, well, you really ought to prefer mutable collections. Okay?

So here, I want to show you an example. So, I'll just copy the same code that I wrote and pasting it here. I'll change the name of the directory to mutable age. Also, we should write mutable mapOf instead of mapOf here. So, now after typing mutable, I'll select mutable mapOf from here and press the tab key on the keyboard. And now, we can add a third element to this array. But this time I will do this edition in the bottom line. So, I'm typing mutable age.put in here in parentheses. Firstly, we will write the key and then the element. So, I'm writing in buffon. No, it's not a French name. But anyway, Buffon's Age here will be 30. So, let's print this to the console, see what we get. So, I'm copying and pasting the print method here. I'm writing buffon here and again I write the buffon as the key.

Now, before running the code, let's put a string expression at the beginning of both outputs. So, here I'll write immutable mapOf. I can copy this and paste it here and then here I'll write mutable mapOf, all right? So now, let's run our code. Cool. So, as you can see we got the age of buffon which we added later to the mutable age array as null in the console because when printing buffon's age, we use the array named age again. But we have to write the mutable age over here, all right, you follow? So, let's fix this right now.

Okay. So, we can now run our codes. And as you can see we have printed the element, we added in later to the mutable age array to the console. So, you can also print to the console using the get method. So, for this you must write get here and you must also write the key in parentheses, all right? So, let's run this code. Now, you can also run your code by pressing the green triangle here. Have you done that yet? And as you can see there's no difference between the two methods, same operations we applied to the set up which we learned in the previous lesson. All right? What do you think of that, my friend?

So, we've learned quite a bit about arrays and collections, but before we wrap this lesson, I would like to mention one more thing. I want you to notice that we have used a print method for each element while printing the elements of all the arrays that we have created so far to the console. So, what if our array consists of a 1000 elements? Should we write a 1000 print methods? Now, have you been thinking ahead? I think perhaps some of you have already posed this question in your head. So, let me just answer it. No, of course not. In Kotlin there is a way to make this process easier. Loops. So, we can print elements of an array to the console very simply just by using loops. Now, before we jump into loops, I do want to give you some intel about operators first. Okay? And then, we'll learn about loops. So, we're going to break here. See you in the next video.


About the Author
Learning Paths

Mehmet graduated from the Electrical & Electronics Engineering Department of the Turkish Military Academy in 2014 and then worked in the Turkish Armed Forces for four years. Later, he decided to become an instructor to share what he knew about programming with his students. He’s currently an Android instructor, is married, and has a daughter.

Covered Topics