hey i have list. I want to update item on top of l...
# android
v
hey i have list. I want to update item on top of list. So which way is efficient in performance, memory management etc.
Copy code
fun main() {
    val listOne = mutableListOf(2, 3)
    listOne.add(0, 1)
    println(listOne)

    var listTwo = mutableListOf(2, 3)
    listTwo = (mutableListOf(1) + listTwo).toMutableList()
    println(listTwo)
}
what's the d/w both example?
r
As long as you’re using a mutable list the most efficient way is the first one. There’s really no point in using it as the second example. If the second example was using immutable lists, like this:
Copy code
var listTwo = listOf(2, 3)
listTwo = (listOf(1) + listTwo)
It would make more sense, and the drawbacks are time and memory since you would be allocating 2 more lists and copying the elements, this is the actual implementation of the plus operator:
Copy code
public operator fun <T> Collection<T>.plus(elements: Iterable<T>): List<T> {
    if (elements is Collection) {
        val result = ArrayList<T>(this.size + elements.size)
        result.addAll(this)
        result.addAll(elements)
        return result
    } else {
        val result = ArrayList<T>(this)
        result.addAll(elements)
        return result
    }
}
But you would be dealing with immutability, which makes a few things easier.
v
great explanation thanks @Rodrigo Lino
i
If you perform many inserts at the beginning of the list it probably won't be so efficient for the default implementation (java.util.ArrayList) since it copies internal array. In this case you may consider using
kotlin.collections.ArrayDeque
or
java.util.ArrayDeque
which have efficient insert operations at both ends (e.g.
addFirst
)
👌 1
s
Why you aren't using Stacks for this
v
@Ivan Pavlov any example for
arrayDeques
@Syed Ovais Akhtar is stacks is better than
kotlin.collections.ArrayDeque
. I am learning to how to improve my code. I don't know about any thing
i
kotlin.collections.ArrayDeque
is a particular implementation of
MutableList
in kotlin. You can use it like any other implementation of
MutableList
. Your example can be written with it like this:
Copy code
val list: MutableList<Int> = ArrayDeque()
list.add(2)
list.add(3)
list.add(0, 1)
v
okk great thanks @Ivan Pavlov
@Ivan Pavlov one question why
addFirst
cannot use in my this implemenation. But in kotlin doc https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-array-deque/
addFirst
is there?
Screenshot 2021-10-19 at 15.14.52.png
i
Because
list
variable has explicit
MutableList
type
val list: MutableList<Int> = ArrayDeque()
If you change declaration to
val list: ArrayDeque<Int> = ArrayDeque()
or omit type
val list = ArrayDeque()
you'll see those methods
v
@Ivan Pavlov so which one you prefer to use
val list: MutableList<Int> = ArrayDeque()
or
val list: ArrayDeque<Int> = ArrayDeque()
in
list.add(0, 1)
or
list.addFirst(1)
both are same or different ?
i
You can take a look at the source code and check difference by yourself 🙂
v
okk great thanks @Ivan Pavlov. Thanks a million ✌️