From the course: Intermediate Kotlin for Android Developers

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Read-only and mutable collections

Read-only and mutable collections

From the course: Intermediate Kotlin for Android Developers

Start my 1-month free trial

Read-only and mutable collections

- [Instructor] Unlike Java, Kotlin has two different interfaces for working with collections. The plain Collection interface handles accessing data and then the MutableCollection interface handles modifying the data. As you can see here, this interface lets you check the size and contents of the Collection as well as iterate over its items, whereas the MutableCollection interface extends it with the additional ability to add, remove, and clear the contents of the collection. Just to illustrate, if we created an array list of integers and declared the variable type to be collection events, then we would receive an unresolved reference error when attempting to invoke the add method. Now this is because the Kotin Collection interface does not contain that particular method, but if we change the return type to be a MutableCollection, then we're good to go and we can add elements to our list of numbers. This type of separation is useful for reducing bugs in your code and making your intent…

Contents