What is the best way to work with queues in java?

2

In what scenarios is a queue necessary? What is the advantage of using this algorithm?

    
asked by anonymous 09.11.2017 / 15:11

1 answer

2

As said by @RHERWOLF in your answer , queue is a data structure. What's the difference between a data structure and an algorithm?

Well, an algorithm is a finite set of steps to do a computation. A data structure composes algorithms (yes, in the plural) and also a place to store the current state of such structure.

Normally, an algorithm is only concerned with its current execution. He simply has to do that processing and quit. A data structure is already concerned with subsequent uses of it. Its status is variable.

For example, to answer what is commonly called a queue, you must implement a rule called FIFO , first in, first out exit).

Only by reading this rule do you notice that the data structure "queue" has at least two operations: push elements into it, remove elements from inside. At no point in this "contract" is spoken of in queue size, then that would be your addition. You could even call it by another name, "measurable queue."

On queue operations, you usually find the following terms:

  • push or enqueue : to add / push elements
  • pop or dequeue : to remove elements
  

What is the best way to work with queues in Java?

So the best way to use a queue is when it fits your problem. It is common for you to already use the classes already coming from the default library because they are "good enough". But it is always possible to have your own solution for queues if you encounter any scenario that would make the system queues bad.

I do not even use a lot of queues at all, they're not usually in my world of trouble at work. Batteries are more common to me.

If you do not need this guarantee of order of arrival and departure, then you do not need a queue. In my work, the most common structure I use is the bag , it's like a set of math but may contain repeated elements.

  

In what scenarios does a queue be necessary?

When you need to store data and make subsequent redemption while maintaining the FIFO constraint.

  

Addendum : In which situations are they not needed?

  • When you do not have a list of values
  • When you do not have the need to store these values
    • I can do statistical analysis of one-dimensional elements without storing each individual, updating their standard deviation and their mean for each new individual that comes up.
  • When you need to store the values, but the redemption order is irrelevant
    • For example, add the values of the items within my order; I do not need these items to be accessed exactly as they were entered in the order; as long as it goes through all items, the order can be completely arbitrary
  • When the access order needs to be inverse to the storage order
    • In this particular case, you need a stack, LIFO , "last in, first out"
  •   

    What is the advantage of using rows?

    When you are dealing with a problem that requires queuing, solving it becomes easier.

        
    09.11.2017 / 23:33