Sets in Kotlin Android

Start course
Overview
Difficulty
Beginner
Duration
1h 43m
Students
23
Ratings
5/5
starstarstarstarstar
Description

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.

Prerequisites

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.

Transcript

Well, hello my friends. So, in our previous lesson, we learned about array lists. So, in this lesson, we're going to learn about set, which derives from the collections class. Actually, you're going to think that it's used a lot like array list. There is one very important difference. In array lists, you can use the same element more than once in the array;  but in sets, only one of the same element is stored. So even if you try to add the same element more than once, in fact, if you notice in our example here, the array that we created using the arrayListOf as four elements and the number two, it's used twice. So, in this case, when we print the size of the rate of console, we get the number four and we can see the last element of the array is two. But when we create the same array using the setOf, well, we get the size of the array to be three, and we can see the last element of the array as three.

You see, so in other words, only one of the two elements written twice is taken into account. The other element is not even noticed. So, if you want to, let's go ahead and open up Android studio and have a little practice. So, first I want to create a new Kotlin file and I'll make the name of this file to be "CollectionsSet". All right so, we'll create our main method. And first, I will create array using the arrayListOf method. So, I'm writing "var mySetArray". I'm writing equal "arrayListOf" and I'll write "Any" as a type of array because this array will consist of different types of element. So, let's let the first element be Kotlin of the type String, second element, let's make it 2 of the type Integer. And we'll make the third element 2.5 of the type Double. And we'll make the fourth element K of type Char. Now, of course we've got to have fifth element and this will be true of the type Boolean. So now, let's use the Kotlin element of the String type once more. All right?

So in other words, we're going to let the 6th element of the array be Kotlin of the type String. Now, I will print two different values to the console. So, the first one will be the size of the array. So, here I'll type "sout" and press 'Enter'. So, I write "mySetArray.size" in parenthesis. If you remember, we were able to learn the size of the arrays using the size method. Right? Let's print the last element of the array. So for this, I want to show you a different method. Here, so after typing "sout" and pressing enter, I'll just write "mySetArray" in parenthesis.   I'll  write ".last", the last method allows us to access a last element of the array. All right so, let's run our code now. And we should see the number six and the Kotlin expression in the console, and what do you know?

Here in the console, you can observe the size of the array. It's six and the last element of the array is Kotlin. So, in other words, we can add the same element more than once to the array using the "arrayListOf". So now let's create the same array using the setOf. So, I'm going to copy and paste the same code that I wrote, and now let's turn these lines of code into the comment line. So here, I'm just going to write "setOf" instead of "arrayListOf". And after typing "set", I choose the setOf expression here and to replace the setOf with the arrayListOf expression here. We can just press the tab key instead of enter on the keyboard. You see how that works. So, we can easily write "setOf" instead of "arrayListOf".

All right so now, let's run our code now. We should see five and true in the console. And as you can see although we define six elements in the array, we observe the size of the array to be five in the console. And of course, the last element in the array is the true expression not Kotlin because even if we use the same element more than once in the arrays  we create using setOf, this gets ignored. All right? So, that is how you can use setOf collection. Right. So, what we'll do is have a short break here, and in our next lesson, we're going to learn about the map collection. All right? So, I'll see you on the other side.

 

About the Author
Students
97
Courses
23
Learning Paths
1

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