From the course: Build Spring Boot Apps with the Kotlin Programming Language

Unlock this course with a free trial

Join today to access over 22,700 courses taught by industry experts.

Immutable lists

Immutable lists

- [Narrator] Now ideally in Kotlin we want to work as much as possible with immutable variables. So the ability to work with genuinely immutable collections is an important requirement. And Kotlin gives us a way to create immutable lists and maps. So we're going to look in this chapter at how we create those. I think we'll create a new file to do this in. And let's call this one exploring collections and we just want to have a main method here. Okay, so the first thing I'd like to do is to create an immutable list. And the way we do that is with the list of key word. So let's create a variable, we'll use the val keyword and I'm going to call my variable colors. And we'll make this a list of the colors, red, green, and blue. Now if I try and do colors dot, what we'll see is there aren't that many methods available to us. There's get the index of an element. There's a method that gives us the first element or null if there are no elements in our list. But, we don't have a dot add or a…

Contents