Why does java linkedlist implementation use the interface deque?

瘦欲@ 提交于 2019-12-05 23:39:01

The LinkedList implementation happens to to satisfy the Deque contract, so why not make it implement the interface?

IIRC, deque stands for double end queue. In the case you mention, it's not logical to define a generic List as a deque. For instance, an ArrayList is not designed for the Deque interface. Insertions will be efficient in the end of the list, but absolutely not at its beginning (because it will cause the re-allocation of a whole array, I think).

The LinkedList is, on the other end, perfectly designed for the Deque interface, as it is a double linked list.

As the JavaDocs states:

These operations allow linked lists to be used as a stack, queue, or double-ended queue.

The List interface is just a List i.e. you can add or remove. So a basic implementation of the List interface has to just provide those simple methods e.g. ArrayList. The Deque interface is the double ended Queue and iava's LinkedList IS-A Double Ended Queue.

Because a double-ended queue might be implemented using something other than a LinkedList and one's code may depend on anything with such functionality, so the Deque interface needs to be available separately.

List should not itself implement/extend Deque because adding to/removing from the start of a list may not be something that can be (easily) supported by every implementation.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!