What is the best way to work with java queues?

Asked

Viewed 790 times

2

In which scenarios is a queue required? What is the advantage of using this algorithm?

1 answer

2


As stated by @RHERWOLF at your answer, queue is a data structure. What is the difference of a data structure and an algorithm?

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

Normally, an algorithm has only its current execution as its concern. It simply has to do that processing and exit. A data structure is already concerned with subsequent uses of itself. Its state is variable.

For example, to meet what is normally called a queue, you must implement a rule called FIFO, first in, first out (first in, first out).

It is only by reading this rule that the data structure "queue" has at least two operations: push elements into it, remove elements from within. At no point in this "contract" does one speak of queue size, so this would be an addition of yours. Could even call by another name, "measurable queue".

About queue operations, you usually find the following terms:

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

How best to work with Java queues?

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

I don’t particularly use queues, they are usually not part of my world of problems at work. Stacks are more common to me.

If you don’t need that order of arrival and exit guarantee, then you don’t need a queue. In my work, the most common structure I use is bag, is like a set of mathematics but can contain repeated elements.

In which scenarios a queue is required?

When you need to store data and make subsequent rescue by maintaining the FIFO restriction.

Addendum: in which situations they are not necessary?

  1. When you don’t have a list of values
  2. When you don’t have the need to store these values
    • I can make statistical analyses of one-dimensional elements without having to store each individual, updating their standard deviation and their average for each new individual that arises
  3. When you need to store the values, but the ransom order is irrelevant
    • for example, adding the values of the items within my request; I don’t need these items to be accessed in the exact way they were entered in the order; as long as you go through all the items, the order can be totally arbitrary
  4. When the access order needs to be reversed to the storage order
    • in that specific case, you need a stack, LIFO, "last in, first out"

What is the advantage of using queues?

When you are dealing with a problem that requires queues, solving them becomes easier.

  • Jefferson, push and pop are terms used in stack, already queued is used enqueue and dequeue and in lists there is a convention. I don’t know what you meant in the sixth paragraph, but it gives a corrected, okay? In addition, the "addendum" excludes many possibilities of no need, it is better to summarize: when you do not need data storage or do not need extraction in order by the former, just as stack is not required when it does not need data storage or need extraction in order by last.

  • It can also explain that data structures have algorithms to manipulate them (insertion algorithm, withdrawal, access, etc.), so structures are not algorithms but are associated with algorithms and are largely differentiated by this. Like, on computing grounds what is the difference in list, queue and stack? The algorithms of storage, access and withdrawal, that’s all.

  • @RHERWOLF, on his second comment, I thought it had become clear that data structure is composed of algorithms and also something more perennial. Are you sure it wasn’t clear? I have the author’s perspective, so if you tell me from the reader’s perspective that I really have sinned, I will try to improve. Perhaps even exemplifying with a concrete model...

  • @RHERWOLF, On your first comment, you made several points. So I’ll try to answer each point. (1) "push and pop are more geared to stack": one is not for language addiction, but I’ve seen in many points using push and pop for queues as well. I will change my answer to by push/enqueue and pop/dequeue. (2) "I don’t know what you meant in the sixth paragraph": Hmmm... what is the sixth paragraph? The one I talk about "about list operations"? If that’s the one, it was my own mistake, it was supposed to be "about operations in a row"

  • (3) "summarize the addendum": thank you for the suggestion. I think it is important to have the 4 items I wrote, but a reduced and direct view is also needed.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.