F
F
foonfyrick2021-03-25 10:17:54
Kotlin
foonfyrick, 2021-03-25 10:17:54

Use always mutable?

Since mutable interfaces add methods on top of simple interfaces for interacting with data, adding, deleting, can they always be used?
That is, instead of listOf() always use mutableListOf() and so on. ? Or how to determine whether to use mutable or not?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Zagaevsky, 2021-03-25
@foonfyrick

Whenever possible, you should use immutable collections, and immutable variables/properties (val, not var). This is necessary in order to make interfaces and code cleaner. It is clearly visible where certain data can change, and where they cannot, for this the collection hierarchy in Kotlin is divided into two parts. Java does not have this, and you had to do checks at runtime (see Collections.unmodifiableList , for example).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question