3

Referencing from the book Java: The Complete Reference, "Queue" interface extend "Collection" interface. Also, "PriorityQueue" extends "AbstractQueue" class and implements "Queue" interface.

Also, as per lots of articles over Internet, Heaps provides the most efficient Priority Queue implementation considering both Insert and Remove in O(logn). And being complete binary tree, heaps can be implemented simply on an array/list.

My question is, If heaps are efficient for Priority Queues, than why PriorityQueue uses a Queue interface? Why it's not using List interface? Otherwise, what is the underlying concept/idea of the implementation, providing time complexity same(or better) as heaps?

3
  • Those are totally different interfaces. A List is wlog. unorderend and allows insertion and deletion at random indices. Queues are ordered and only provide methods to add elements such that the queue-property is not violated and to peek &pop the topmost element. Commented Mar 3, 2021 at 22:23
  • 1
    Because it implements a queue, which is something you add to the end of and take from the beginning of, and not a list, which is something you iterate over, add and remove in the middle of, etc. There is no reason why the interface needs to reflect the underlying implementation. Commented Mar 3, 2021 at 23:13
  • 1
    You should try to understand the difference between implementing an interface and internally using a data structure. Commented Mar 5, 2021 at 8:15

2 Answers 2

2

Interfaces are "contracts" which ensure that classes implementing them will provide certain "behavior" - methods.

So client code (code that will make use of PriorityQueue for example) will have guarantees that this class will have behavior that may be expected only from Queue implementation.

In other words:

List interface has methods that are useful to work with lists.

Queue interface has methods that are useful for work with queues.

PriorityQueue that implements methods of a List interface just won't be comfortable to work with.

And vice versa, imagine ArrayList that implements Queue interface - it will be chaos to work with such thing.

More related to your question: There may be multiple implementations of Queue interface - on array, on ArrayList or LinkedList with different time complexity and so on, but all of them will provide same set of methods of Queue interface.

Sign up to request clarification or add additional context in comments.

Comments

2

Because the PriorityQueue is just a Queue with the Heapify technique to guarantee good performance during insertion/deletion.

Heap means Heapify technique

Why PriorityQueue uses the Heapify technique?

  • Heapifying is a method for inserting/deleting with the worst case of O(log2 N)

What is the inheritance hierarchy of PriorityQueue?

public class PriorityQueue<E> extends AbstractQueue<E> {}
public abstract class AbstractQueue<E> extends AbstractCollection<E> {}
public abstract class AbstractCollection<E> implements Collection<E> {}

What is the inheritance hierarchy of List?

public interface List<E> extends Collection<E> {}

Why the PriorityQueue does not extend the List interface?

Because the List interface provides operations not defined or needed to the PriroityQueue like get(index), and the implementation just add() and poll() which similar to the queue mechanism which is insertion to the tail and poll from the head, and not sequential insertion like ArrayList or LinkedList which are extending the List interface. And no ability to get the values with index position.

What is the difference between PriorityQueue and Queue?

PriorityQueue uses the "Heapify" technique and can be Min PQ or Max PQ and you can simply define that when you declare the constructor of the PriorityQueue, while the Queue just adds the values to the tail and poll from the head.

What are the benefits to use PriorityQueue?

The big benefit is you can define Min PQ or Max PQ and get the elements in sorting order ascending or descending with time complexity O(log2 N), and this important for many applications like finding the shortest path, collision of particles detection, etc.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.