0

How to declare an empty mutable collection(List) of generic type in kotlin?

1 Answer 1

4

Have a look at the Kotlin reference regarding Collections: List, Set, Map. The following will create an empty mutable list of your desired type:

val yourCollection = mutableListOf<YourType>()

If you rather meant a mutable Collection<List<YourType>>, then maybe the following will help you:

val mutableCollectionWithList : Collection<List<YourType>> = mutableListOf(mutableListOf())

Alternatively if you do not really need an empty list you can also do:

val yourCollection = mutableListOf(YourType(), ...)

And if you rather want an empty list which is not mutable, use the following instead:

val emptyList = emptyList<YourType>()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.