Check out a free preview of the full Introduction to Kotlin and Android Development course:
The "Collections" Lesson is part of the full, Introduction to Kotlin and Android Development course featured in this preview video. Here's what you'd learn in this lesson:

Maximiliano discusses collections including mutable and immutable lists, sets, maps, and dictionaries. When talking to the Android SDK sometimes Java arrays are required.

Get Unlimited Access Now

Transcript from the "Collections" Lesson

>> Collections, so Kotlin includes several collections ready to use, as we expect from a modern language. So we're not going to cover all of them. So let's cover quickly the ones that we will use during the rest of the day. We have list, a list is what we typically know as an array, okay, but here we have the type list.

[00:00:22] We define the type with angle brackets, okay? This is actually a generic, in case you know what the generic is. So we define an array of countries or a list of strings. And unfortunately, if you're used to JSON, we don't use that syntax for literals. We use a constructor that has the name listOf.

[00:00:45] So listOf, it's actually like in JavaScript, opening square brackets and setting the countries, okay, like this. But here we use listOf. It's a constructor, it's a global constructor that returns a list of the options that you have, okay? By default a list is immutable. So after I set the list, I cannot remove or add elements to it unless I create a mutable list.

[00:01:19] That is another type. So the mutable is list has all the methods to add, replace, or remove elements. So you pick when you make a list, if you want it mutable or unmutable okay? We'll have sets, okay? I said, the difference is that it cannot contain duplicates. So in this case, strings will have three elements because C is going to be store once only, and there is no order in a set.

[00:01:51] We have a map that is kind of a dictionary, like when you have a key and value. And we also have arrays, and you say [SOUND] we call it list. Well, actually, this is for Java. Sometimes, I mean, this is actually probably low level. We're not going to use it today, but I'm just going to mention that quickly, or briefly, we mentioned that the SDK is written in Java.

[00:02:18] So from Kotlin, when you're talking to the Android SDK, sometimes you need to make some conversions. It's not a big deal, but for example it happens on lists. So there is a quick way to convert the list into an array, that it's a similar structure, but it's not exactly the same.

[00:02:40] So that's why we need to convert that into arrays when we are working with the SDK. Again, not for today, we will stay within Kotlin today and it will work. Any question on collections?
>> Did you say one of these can't have duplicates?
>> The set.
>> The set.

>> The set cannot have duplicates. Remember the idea of Venn diagrams from college? That's a set, so you cannot have two of the same kind, there is no order. That happens with sets on different languages.