20

MutableList is an interface, but I cannot find a class in kotlin package that implements it explicitly. Is there any? Furthermore, I would have expected to be a package-scope defined mutableListOf(varargs) symetrically to listOf(varargs). Up until now, I have to use java Collections.

0

3 Answers 3

27

Try to use mutableListOf or arrayListOf.

Sign up to request clarification or add additional context in comments.

3 Comments

It would be nice if this, the accepted answer, explained this a bit more. That List and Mutable list are a bit of Kotlin magic. And that all Java List implementations are both Kotlin List and MutableList including ArrayList and LinkedList along with other custom implementations. Kotlin has helper functions to create lists more easily as shown in this answer. Other helpers such as listOf return the read only list interface List instead of the mutable interface. THe ones in the answer return the MutableLis interface.
linkedListOf is gone before 1.0. There is more generic mutableListOf available instead.
they are only shortcuts, mutableListOf simply returns an ArrayList (which is mutable); listOf is an alternative to call Arrays.asList (which is immutable)
0

You are right, java collections are the only way to get MutableList

Comments

0

No, as far as Collections are concerned you can not avoid java API's, Because every kotlin collection uses corresponding java collection in one way or another.

Kotlin collection interfaces are just mock interfaces, they don't really exist at runtime. So even if you had a class implementing some kotlin collection interface(ex. MutableList) at runtime it would use corresponding java interface.

for example let's write two kotlin classes implementing kotlin's List and MutableList interfaces respectively

abstract class KotlinList : List<Int>

abstract class KotlinMutableList : MutableList<Int>

when you decompile these classes their signature's change as following (heads up, byte code ahead)

public abstract class com/example/kotlin/KotlinList implements java/util/List kotlin/jvm/internal/markers/KMappedMarker

public abstract class com/example/kotlin/KotlinMutableList implements java/util/List kotlin/jvm/internal/markers/KMutableList

Now both the classes implement java.util.List interface and they also implement kotlin.jvm.internal.markers.KMappedMarker and kotlin.jvm.internal.markers.KMutableList interfaces respectively, so as you can see kotlin interfaces has disappeared and we are left with same old java List and your methods calls will now be addresses as java.util.List.someMethod and not with some kotlin interface. KMutableList and KMappedMarker as their name suggests are marker interfaces used by the the compiler.

In case of KotlinList class if you look inside you will see all mutater methods of java.util.List are generated for you and look similar to the one below

   public boolean add(int var1) {
      throw new UnsupportedOperationException("Operation is not supported for read-only collection");
   }

So as you can see there is no way of avoiding java API's, because they are the foundation of kotlin's collections.

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.