Queue and Deque interfaces in Java
Comparison of Queue and Deque interfaces.
Queue is a queue, which is usually (but not necessarily) built on the FIFO (First-In-First-Out) principle - accordingly, the element is retrieved from the beginning of the queue, and the element is inserted at the end of the queue. Although this principle is violated, for example, PriorityQueue, using "natural ordering" or passed Comparator when inserting a new element.
Deque (Double Ended Queue) extends Queue and according to the documentation, it is a linear collection that supports insertion/retrieval of elements from both ends. In addition, implementations of the Deque interface can be built on the FIFO or LIFO principle.
Both Deque and Queue implementations usually do not override equals() and hashCode() methods, but instead use the inherited methods of the Object class based on reference comparison.
Why does LinkedList implement both List and Deque?
LinkedList allows elements to be added to the beginning and end of the list in constant time, which is well consistent with the behavior of the Deque interface.
LinkedList is a singly linked, doubly linked or four linked list?
Doubly linked: each LinkedList element stores a link to the previous and next elements.
How to iterate over the elements of a LinkedList in reverse order without using slow get(index)?
To do this, LinkedList has a reverse iterator that can be obtained by calling the descendingIterator() method.
What does PriorityQueue allow you to do?
PriorityQueue feature is the ability to control the order of elements. By default, items are sorted using "natural ordering", but this behavior can be overridden using the Comparator object that is specified when the queue is created. This collection does not support null elements.
Using PriorityQueue, you can, for example, implement Dijkstra's algorithm to find the shortest path from one vertex of the graph to another. Or to store objects according to a certain property.
Stack is considered "obsolete". What is recommended to replace it with?
Stack was added in Java 1.0 as an implementation of the LIFO (last-in-first-out) stack and is an extension of the Vector collection, although this violates the concept of a stack somewhat (for example, the Vector class provides the ability to access any element by index). It is a partially synchronized collection (except for the add method push()) with the ensuing consequences in the form of a negative impact on performance. After adding the Deque interface in Java 1.6, it is recommended to use implementations of this particular interface, for example, ArrayDeque.
Read also:
- ArrayList and LinkedList in Java
- Iterator interface in Java
- ArrayList and LinkedList in Java, memory usage and speed
Comments
Post a Comment