No one likes to stand in line¹. Most people hate to waste their time queuing up. We spend an average of 6 months of our adult lives waiting our turn — almost 3 days a year. There are books about queues. Documentaries². Even academic experts in queues. But then, when we are coding, we often forget to use
queues and end up messing around with
All things work better when you use the right tool.
I used draw.io to draw this diagram. Awesome tool!
When implementing FIFO queues we usually use
peek(returns, but does not remove, the head of the queue) and
poll(returns and removes the head of the queue) methods, while we refer to them, respectively, as
popin **LIFO **stacks.
stack we can only add and remove elements from one end (LIFO), in a
queue we add elements to one end and remove them from the other (FIFO) and, joining those two worlds we have the
deque (aka “Double Ended Queue”) where we can add and remove elements from both ends. None of them allows random access to the elements.
In JAVA you should use
dequeto model a
stackbecause the Stack class is considered obsolete (it extends the Vector class and inherits all its methods, making it possible to break the LIFO principle).
So, don’t reinvent the wheel⁴.
This article was originally published on Medium
 Ok, there are some weird exceptions
 There is always an alternative https://www.youtube.com/watch?v=ZyNz8UHHrxE (in Spanish)
 Some people know it as FCFS (“First Come, First Served”)
 And remember to give LolaMarket a chance. Awesome service!