lesincs
07/04/2021, 9:31 AMmutableList.add(0,element),
are there any other APIs to insert an element to the head of a mutablelist
? mutableList.add(0,element)
is ok, but not as expressive. 🤔CLOVIS
07/04/2021, 9:49 AMadd(0, element)
?Roukanken
07/04/2021, 10:21 AMadd(0, e)
is not expensive by it's definition, but by implementation of MutableList - for example LinkedList has effective implementation of it (but sacrifices a lot of other functions)
There are several solutions, depending on what you want to be doing:
a) LinkedList, (but get and stuff is usually way more important, and linked list sucks at nearly everything else)
b) reverse, add N elements at end, reverse - if you want to add lot of elements at start at once, this is simple and basically as effective as adding at the end
c) "Circle List"/Array dequeue, not sure if there is some implementation of it, but you can implement something very similar to ArrayList, but it can push/pop from both front and back very effectively.
For the c) thing, as I'm not entirely sure about it's official name here is simple explanation:
You store a simple array, index of start, and index of end of the data. If you want to access an element at index
, you look at (start + index) modulo total_array_size
→ eg, after last element of array, you consider next at the first position. (unless it's the last index)
This way, you will have an allocated memory both before & after real data, so you can do push/pop simply with manipulating the stored start/end indices and storing one element. If real size == total size
you can simply allocated array double the size and copy the whole real data to it → similar trick to what ArrayList uses.
Note that REALLY consider what are you doing, as usually it's not worth to do such optimizations in real-world application, unless this manipulation is on your "hot codepath" - the thing that does the vast majority of total workdiesieben07
07/04/2021, 10:24 AMArrayDeque
is in the Kotlin common stdlibRoukanken
07/04/2021, 10:26 AMlesincs
07/04/2021, 12:58 PMmutableList.apply {
// do some calculations
val element = .....
add(0, element)
}
.xxx { }
It works fine. But I’m wondering if there’s some other APIs like this:
mutableList.insertElementToHead {
// do some calculations
val element = ...
element
}
.xxx {}
It doesn’t seem like there are so many differences between the two ways. But I prefer the second way, when other people read the code, they can know what I do at the first glance.
Maybe I’m kind of paranoid? Of course I can write an extension function for the MutableList to do this, but just in case there are other existing ways.Sourabh Rawat
07/04/2021, 2:29 PMread the code, they can know what I do at the first glancedepends on who is reading the code. Most of the programmer won't have issues in understanding
add(0, item)
means addFirst(item)
. Sure, you can have addFirst
APIs. But for normal List, its not needed specifically. Things like Deque
provides them, because there it makes sense to have a special function which is doing add(0, item)