Is there an instanciable MutableList in Kotlin, avoiding java api?

kotlin mutable list to list
convert mutablelist to arraylist kotlin
kotlin mutable list example
mutablelist vs arraylist kotlin
kotlin map
kotlin list
kotlin immutable list
kotlin mutable list add

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.

Try to use mutableListOf or arrayListOf.

Exposing a Mutable member as Immutable, A generic ordered collection of elements that supports adding and removing elements. Parameters. E - the type of elements contained in the list. The mutable list is� For now, I have to use java Collections. Answer: Try to use arrayListOf or linkedListOf. Related: Is there an instancible MutableList in Kotlin, avoiding java api? kotlin MutableList is a trait, but I cannot find a class in kotlin package that implements it explicitly. Is there any?

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

MutableList, To avoid excessive memory usage, use alternative ways. For example, you can create a mutable map and populate it using the write operations. The apply()� Calling Java from Kotlin. Calling Kotlin from Java. JavaScript. Setting Up a Project. Dynamic Type. The mutable list is invariant on its element type. Functions.

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.

Constructing Collections, inline fun <T> MutableList( size: Int, init: (index: Int) -> T ): MutableList<T>. Creates a new mutable list with the specified size, where each element is calculated� Calling Java from Kotlin. Calling Kotlin from Java. JavaScript. Setting Up a Project. Dynamic Type. MutableList < T > Returns an empty new MutableList.

mutableListOf, Returns an empty new MutableList. fun <T> mutableListOf(vararg elements: T): MutableList<T>. Returns a new MutableList with the given elements. class ArrayList < E > : MutableList < E >, RandomAccess, AbstractMutableCollection < E > Provides a MutableList implementation, which uses a resizable array as its backing storage. This implementation doesn't provide a way to manage capacity, as backing JS array is resizeable itself.

Kotlin中是否有一个不可思议的MutableList,避免了Java API?, Is there an instanciable MutableList in Kotlin, avoiding java api?MutableList是一个接口,但是我无法在kotlin包中找到明确实现该类的类。 Calling Java from Kotlin. Calling Kotlin from Java. JavaScript. Setting Up a Project. MutableList < Double > fun BooleanArray. toMutableList (): MutableList

Kotlin中是否有一个instableMutableList,避免java api?, MutableList是一个接口,但我无法在kotlin包中找到一个明确实现它的类.有没有 Is there an instanciable MutableList in Kotlin, avoiding java api? In Kotlin, the default implementation of List is ArrayList which you can think of as a resizable array. MutableList is a List with list-specific write operations, for example, to add or remove an element at a specific position. Let's demonstrates the usage of important List interface methods with an example.

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)