Skip to main content
Do checkout my poem section. You are going to love it.

Priority Queue

 A Priority Queue in Java is a specific type of queue implementation where elements are dequeued (removed) based on their priority, rather than the standard First-In, First-Out (FIFO) order.

🥇 Priority Queue Summary

  • Structure: It is based on the heap data structure, specifically a Min-Heap by default, where the smallest element is at the root. You can customize it to be a Max-Heap using a Comparator.

  • Ordering: Elements are ordered according to their natural ordering (if they implement Comparable) or by a Comparator provided at the time of construction.

  • Priority:

    • In a Min-Heap (default), the element with the lowest value is considered the highest priority and is retrieved first.

    • In a Max-Heap, the element with the highest value is considered the highest priority.

  • Implementation: In the Java Collections Framework, it is implemented by the java.util.PriorityQueue class, which is part of the Collection and Queue interfaces.

  • Time Complexity:

    • Insertion (add/offer): O(log n)

    • Removal (remove/poll): O(log n)

    • Inspection (peek): O(1)

  • Capacity: It is an unbounded queue, but it has an internal capacity that automatically increases as elements are added.

  • Thread Safety: It is not synchronized (not thread-safe). If you need thread-safe priority queue, use java.util.concurrent.PriorityBlockingQueue.

⚙️ Key Methods

The PriorityQueue implements the Queue interface, so it uses the standard queue methods, but their behavior reflects the priority-based ordering.

MethodPurposeReturnsTime ComplexityNotes
add(E e)Inserts the specified element into the priority queue.boolean (always true in PriorityQueue)O(log n)Throws IllegalStateException if no space is available (not usually an issue as it's unbounded).
offer(E e)Inserts the specified element.boolean (always true in PriorityQueue)O(log n)Preferred over add() for capacity-constrained queues (though PriorityQueue is unbounded).
peek()Retrieves, but does not remove, the head (highest priority element) of this queue.E (element) or nullO(1)Returns null if the queue is empty.
element()Retrieves, but does not remove, the head of this queue.E (element)O(1)Throws NoSuchElementException if the queue is empty.
poll()Retrieves and removes the head (highest priority element) of this queue.E (element) or nullO(log n)Returns null if the queue is empty. Preferred over remove().
remove()Retrieves and removes the head of this queue.E (element)O(log n)Throws NoSuchElementException if the queue is empty.
remove(Object o)Removes a single instance of the specified element, if it is present.booleanO(n)Not efficient as it searches linearly.
size()Returns the number of elements in the queue.intO(1)

 PriorityQueue<Patient> hospitalQueue = new PriorityQueue<>( (p1, p2) -> Integer.compare(p2.severity, p1.severity) );

Comments

Popular posts from this blog

SAP BASIS

Hey Learners, I am glad you found this. Now rest your thoughts as your search stops here for the basis content hunt. How to use this: Keep your SAP system open to refer to whatever is mentioned here Keep a notebook and pen to mention those parts you don't know. And mark down those topics. Take time for each topic, and it's better to have a friend to discuss the topics in here. Happy Learning! Don't forget to comment on your status.     Click on the above image

An experiment with the life

"Best Thing about experiment is that it only improves the outcome." Well, I am Rakshit, hope you already know. I am not special and surely not especially gifted. Neither things go according to my wish. Neither I am the best writer.  But I am myself who is totally unique from anyone else. And I am Rakshit Ranjan Singh. I have my own fun, fights and fall in the most fundamentalistic way. Mechanical is my degree. IT is my Job. Beauty in nature is what I search. Words of my heart are what I write. Four different things I carry on my shoulder and a smile on my face, hope you might have seen that. What do I care for? Family, friends and nature. Do I have regrets? More than I can imagine. Let us move further to see what really is my life.

Learn Java

Hello Friends, You might already know what Java is. Without taking much of your time, I would like to ask you to please click below if you are ready to learn it from end to end. The Material over here is available on the internet and is free to access.  I would request you to bookmark this page and follow it. Please comment if you are happy with the learning. click here