'List clear and addAll in single statement Kotlin
Is there any method in kotlin which replaces below two line into one. I know i can create a extension function but I'm eager to know if it already exist in kotlin. Something like listOfChecklist.clearAndAddAll()
.
listOfChecklist.clear()
listOfChecklist.addAll(newList)
This is what I'm doing now manually using an extension function. But I hope there is a better solution.
fun <E> MutableCollection<E>.clearAndAddAll(replace: MutableSet<E>) {
clear()
addAll(replace)
}
Solution 1:[1]
It's not possible to do this. An idiomatic way to solve your case would be using a scope function like with
.
val list = mutableListOf<String>()
with(list){
clear()
addAll(arrayListOf())
}
Solution 2:[2]
My answer will be rather philosophical, so here are my thoughts:
If you really feel the need to merge these 2 operations into one line of code, it suggests you use this very often.
If you use this design pattern very often, it seems to me that there is something quite wrong in your overall design of your application/class and here I totally agree with the "I hope there's no such method" comment by @miensol.
If you don't use it often, I think that leaving it on 2 lines of code makes it much more readable.
Clearing the "listOfChecklist" makes sense only if "newlist" gets cleared at some point too, otherwise you could just keep the reference like: "listOfChecklist = newlist" and boom! You're done ...
If you use mutable lists a lot, which are being cleared and their elements are being copied between each other, again, it seem to me as an over-use of mutable state, which makes your code difficult to reason about. I would suggest to look at some core functional programming principles which will get you rid of the clear/addAll concerns completely :-).
I am not a performance expert, but for most of the use cases I don't really see a reasonable benefit of reusing an existing list instead of just simply cloning it (if you really need to keep the elements for some reason) and speaking of performance itself, the JVM almost always does a good job anyway.
Conclusion: What I would suggest is to keep it on 2 lines if you really need it or design your code in a way so that you completely avoid the need for clear/addAll. Otherwise you can just go with your extension function or whatever makes you feel happy.
Solution 3:[3]
If you want better performance don't do "clear+addAll". Consider using wrapper and call wrapper.list=newList
.
this way you can share reference to its instance and still can replace all items in one atomic operation and avoid data race.
Please look what is inside: In case of Android with ArrayList working as mutableListOf() by default: clear() 1000 rows and addAll() 100 rows "array" inside list object will not free your memory, it executes elementData[I]=null 1000 times and 100 write operations with new elements.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | LittleLight |
Solution 3 |