13

Maybe it's silly, but I have to know the answer. I am scratching my head looking at its source code and doesn't see any reason why authors implement Queue in LinkedList, but decided not to do the same with ArrayList, instead they have created separate class ArrayDeque.

Lii
  • 11,553
  • 8
  • 64
  • 88
ieXcept
  • 1,088
  • 18
  • 37

3 Answers3

9

The interface Queue requires that add adds items to the end of the Queue and remove takes elements from the start of the Queue.

(pseudo code)

Queue<String> q = ...
q.add("A")
q.add("B")
q.add("C")
//q is now [A,B,C]
String a = q.remove()
// a is A and q is [B, C]

Now; with an ArrayList, the remove operation would be O(n) - I would imagine that the API designers decided that this performance is unacceptable.

remove is O(n) because it requires reindexing the whole list - B is now 0 and C is now 1. LinkedList does not have this problem as it uses a linked list datastructure; it just remove the head node and sets the child of that node to be the new head node.

ArrayDeque is a different design to support this in O(1) - it is therefore not a List.

Boris the Spider
  • 59,842
  • 6
  • 106
  • 166
1

Most likely because it wouldn't be very performant as a Queue, since adding to (well, removing from in the case of Queue) the beginning is an O(N) operation instead of O(1). ArrayDeque on the other hand is a different design, since it's not a List and therefore doesn't need to provide random access.

Kayaman
  • 72,141
  • 5
  • 83
  • 121
  • `ArrayList` predates `Queue` by many years and several versions. One does not simply extend the contract of a type just because a new interface exists. Especially when it is of questionable value to do so. `LinkedList`, OTOH, was worth the effort. – Lew Bloch Jan 15 '17 at 21:55
  • 1
    @LewBloch That's not exactly true. There are plenty of older classes that have been modified to implement a newer interface where it makes sense. Using an `ArrayList` as a queue is possible, but it's not very smart. – Kayaman Jan 16 '17 at 06:31
  • Yes, there are, like `LinkedList`, as I mentioned. – Lew Bloch Jan 16 '17 at 19:49
  • @LewBloch I'm unclear why you decided to comment with such a contradictory statement. There was no effort. It was very simple to make `LinkedList` implement `Queue`. – Kayaman Jan 16 '17 at 19:55
  • Of course, and I didn't say that `LinkedList` required much effort. That was my original point, that it did *not* require much effort. How is that contradicting anything? – Lew Bloch Jan 16 '17 at 19:57
  • Of course it is not true that adapting `LinkedList` required _no_ effort. It required some, naturally, particularly testing. No change requires zero effort. – Lew Bloch Jan 16 '17 at 20:02
0

As Boris the Spider said, I am continuing his explantation.

Now, when we remove an item from the beginning and the end of an array or list, it takes O(1) running time, because it just goes there and removes it, no need to use the loop.

But, in the queue, if we use ArrayList.remove() method, it will remove the last element, it violates the principle of the queue, cause first in first out, we need to remove the first element as per the principle, which is not achievable with ArrayList. To delete the first item with the remove() method, we have to reach index 0 by using a loop then we can delete the value of that index. That takes O(n) running time. So, it is not beneficial for us.

We can use PriorityQueue(), LinkedList() implementation with Queue interface.