Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How are Kotlin Array's toList and asList different?

The Kotlin Array class offers asList(), toList(), and toMutableList() methods. The first two methods both return a List and are described in the Kotlin reference as follows:

  • asList() returns a List that wraps the original Array.
  • toList() returns a List containing all elements [of the original Array].

These methods appear interchangeable. How do these two methods differ in practice?

like image 608
M. Palsich Avatar asked Jan 23 '18 21:01

M. Palsich


People also ask

What is the difference between Arrayof and listOf in Kotlin?

Array is of fixed size. It cannot increase and decrease in size. MutableList<T> do have 'add' and 'remove' functions in order to increase or decrease the size of the MutableList. Use it for better performance, as array is optimized for different primitive data types such as IntArray[], DoubleArray[].

How is array asList () different?

1) ArrayList internally uses a dynamic array to store the elements. LinkedList internally uses a doubly linked list to store the elements. 2) Manipulation with ArrayList is slow because it internally uses an array. If any element is removed from the array, all the other elements are shifted in memory.

What is the use of Arrays asList method?

The asList() method of java. util. Arrays class is used to return a fixed-size list backed by the specified array. This method acts as a bridge between array-based and collection-based APIs, in combination with Collection.

Can Arrays asList be modified?

Arrays. asList() method returns a fixed-size list backed by the specified array. Since an array cannot be structurally modified, it is impossible to add elements to the list or remove elements from it.


2 Answers

TL;DR

The list created with asList keeps a reference to the original Array.
The list created with toList/toMutableList is backed by a copy of the original Array.

Explanation

asList

The asList function creates a list that reuses the same Array instance, which implies that changes to the original array also have impact on the List:

val arr = arrayOf(1, 2, 3) val l1 = arr.asList()  arr[0] = 4 println(l1) // [4, 2, 3] 

toList

This isn't the case for toList/toMutableList since the array is copied:

val arr = arrayOf(1, 2, 3) val l2 = arr.toList()  arr[0] = 4 println(l2) // [1, 2, 3] 

The Kotlin source code can be found here.

like image 180
s1m0nw1 Avatar answered Sep 28 '22 21:09

s1m0nw1


Basically asList() still maintains a reference to the original Array. That means mutations to that list will also mutate the underlying Array.

toList() is simply copying the values of the Array into a new List, but there is no lingering link afterwards.

For most use-cases, they probably are interchangeable. asList() will likely have slightly better performance (since it isn't performing a copy) and toList() will be a "safe" copy against unexpected mutations.

like image 34
samanime Avatar answered Sep 28 '22 22:09

samanime