Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use queue over arraylist

Tags:

One basic argument to use a Queue over an ArrayList is that Queue guarantees FIFO behavior.

But if I add 10 elements to an ArrayList and then iterate over the elements starting from the 0th element, then I will retrieve the elements in the same order as they were added. So essentially, that guarantees a FIFO behavior.

What is so special about Queue as compared to traditional ArrayList?

like image 561
Victor Avatar asked Jul 02 '13 21:07

Victor


People also ask

When would you choose to use ArrayList over?

ArrayList provides constant time for search operation, so it is better to use ArrayList if searching is more frequent operation than add and remove operation. The LinkedList provides constant time for add and remove operations. So it is better to use LinkedList for manipulation.

Why and when should I use stack or queue data structures instead of arrays lists?

We use stack or queue instead of arrays/lists when we want the elements in a specific order i.e. in the order we put them (queue) or in the reverse order (stack). Queues and stacks are dynamic while arrays are static. So when we require dynamic memory we use queue or stack over arrays.

What is the difference between a list and a queue?

The main difference between a List and a Queue is that while the List has a single integer to remember how many elements are actually stored in the array (the internal count), a Queue has a count as well as a start index. The queue uses the internal array as a ring buffer.


2 Answers

You can look at the javadoc here. The main difference is a List lets you look at any element whenever you want. A queue only lets you look at the "next" one.

Think about it as a real queue or as a line for the cash register at a grocery store. You don't ask the guy in the middle or the end to pay next, you always ask the guy who's in the front/been waiting the longest.

It's worth noting that some lists are queues. Look at LinkedList, for example.

like image 118
Daniel Kaplan Avatar answered Oct 04 '22 23:10

Daniel Kaplan


If I gave you a Queue instance then you would know that by iteratively calling remove() you would retrieve the elements in FIFO order. If i gave you an ArrayList instance then you can make no such guarantee.

Take the following code as an example:

        ArrayList<Integer> list = new ArrayList<Integer>();     list.add(5);     list.add(4);     list.add(3);     list.add(2);     list.add(1);       list.set(4,5);     list.set(3,4);     list.set(2,3);     list.set(1,2);     list.set(0,1);      System.out.println(list); 

If I were now to give you this list, then my iterating from 0 to 4 you would not get the elements in FIFO order.

Also, I would say another difference is abstraction. With a Queue instance you don't have to worry about indexes and this makes things easier to think about if you don't need everything ArrayList has to offer.

like image 34
Lee Martie Avatar answered Oct 04 '22 23:10

Lee Martie