As a developer everybody in their careed must have faced the requirement where you need resizable collection where you can do add, remove, retrievale(FIFO).
In every application i see using List(ArrayList) to satisy this requirement but my question why not developer go for Queue(probably ArrayDeque) . As per my current understanding i find both ArrayList(List) and ArrayDeque(Queue) equally good for the requirement i stated. But still i never spotted queue in my career , always find only List.
So my question is why not Queue is preferred over List. I am sure there must be some reason but somehow i am missing that understanding?
Update:- here is my clear requirements
1)Addition happens at end and should be fast.Probably O(1)
2)Iteration should be fast
3)lookup and removal of any specific element should be faster.
Going by above requirement i think Arralist makes sense over ArrayDeque . Here are my pointwise reason
1)Both Arraylist and ArrayDeque will be O(1) . Right?
2)Iteration performance will be same for both as it will be based on index . For ArrayDeque index will be based on timestamp whereas for arraylist user can explicitly mention index. Right?
3)It will be O(1) for both as lookup will happen based om index
I prefer using whatever reference will give me the minimal set of functionality I need for easiest migration if I change implementations.
If I just need to add and remove, I can just do
If I need to get them in FIFO order, I won't be using a
List
because theList
interface has no methods for popping the oldest. It has a method for popping index 0, but that's not the same. If I need FIFO ordering, I must resign myself to using a Queue reference.Most Queue and Deque implementations are lists themselves, so it makes sense that it might be confusing. But you do not want to do this:
Instead, use a reference with the minimal amount of functionality necessary to do the job. In this case, that means using the interface and not the implementation specific methods of the
ArrayDeque
class.